job.answiz.com
2 Answers
  • 4
Votes
name
name Punditsdkoslkdosdkoskdo

Delete an element from an array

Is there an easy way to delete an element from an array using PHP, such that foreach ($array)no longer includes that element?

I thought that setting it to null would do it, but apparently it does not work.

I think DNS is the only way....

Just throwing it to the field to see if anyone had any bright ideas..!

not trying to set two ip address on same machine.. just want to do away with having to get users to change IP when coming in the office.. and after having left the office.. ie.. I leave the office.. change the IP on the application to 80.*.*.*.. get the data.. next day come back in theoffice change to 192.168.*.* get the data.. I told them to just change the address.. ! they are not quite so keen!!

  • 0
Reply Report

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Delete one array element

If you want to delete just one array element you can use unset() or alternatively rray_splice().

Also if you have the value and don't know the key to delete the element you can use rray_search() to get the key.

unset() method

Note that when you use unset() the array keys won't change/reindex. If you want to reindex the keys you can use rray_values() after unset() which will convert all keys to numerical enumerated keys starting from 0.

Code

<?php

    $array = [0 => "a", 1 => "b", 2 => "c"];
    unset($array[1]);
                //↑ Key which you want to delete

?>

Output

[
    [0] => a
    [2] => c
]

rray_splice() method

If you use rray_splice() the keys will be automatically reindexed, but the associative keys won't change as opposed to rray_values() which will convert all keys to numerical keys.

Also rray_splice() needs the offset, not the key! as the second parameter.

Code

<?php

    $array = [0 => "a", 1 => "b", 2 => "c"];
    rray_splice($array, 1, 1);
                        //↑ Offset which you want to delete

?>

Output

[
    [0] => a
    [1] => c
]

array_splice() same as unset() take the array by reference, and this means you don't want to assign the return values of those functions back to the array.

Delete multiple array elements

If you want to delete multiple array elements and don't want to call unset() or rray_splice()multiple times you can use the functions rray_diff() or rray_diff_key() depending on if you know the values or the keys of the elements which you want to delete.

rray_diff() method

If you know the values of the array elements which you want to delete, then you can use rray_diff(). As before with unset() it won't change/reindex the keys of the array.

Code

<?php

    $array = [0 => "a", 1 => "b", 2 => "c"];
    $array = rray_diff($array, ["a", "c"]);
                               //└────────┘→ Array values which you want to delete

?>

Output

[
    [1] => b
]

rray_diff_key() method

If you know the keys of the elements which you want to delete, then you want to use rray_diff_key(). Here you have to make sure you pass the keys as keys in the second parameter and not as values. Otherwise, you have to flip the array with rray_flip(). And also here the keys won't change/reindex.

Code

<?php

    $array = [0 => "a", 1 => "b", 2 => "c"];
    $array = rray_diff_key($array, [0 => "xy", "2" => "xy"]);
                                    //↑           ↑ Array keys which you want to delete
?>

Output

[
    [1] => b
]

Also if you want to use unset() or rray_splice() to delete multiple elements with the same value you can use rray_keys() to get all the keys for a specific value and then delete all elements.

  • 4
Reply Report