PHP: Removing an item from an array

If you have a php array, but need to nuke an item from it, the unset function is just the tool to do that. When iterating through the array after removing the item, you should be slightly careful.

The unset function does remove the item, but it doesn’t “reindex” the array, so you should traverse the array by index-numbers after removing the item. The issue is probably best illustrated by this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$myArray = Array('item 1', 'item 2', 'item 3');
 
// remove item
var_dump($myArray);
 
unset($myArray[1]);
var_dump($myArray);
 
// Failing bad loop
for ($loop = 0; $loop < count($myArray); $loop++) {
        echo $myArray[$loop] . "\\n";
}
 
// Good loop
foreach ($myArray as $item) {
        echo $item . "\\n";
}

You can dowload the above example code here.

  • Correct, you could use array_diff() to delete an element if you know what the elements are that you are deleting. I chose to use an array_filter() though because you can do conditional deleting. For example, you can delete it only if it’s not numeric or if it’s greater than 5 characters.

    You can’t perform conditional deletes using an array_diff() but it is a simple way to delete items you do know the value.. Thanks for another quick way to delete an item from an array in php.

  • Correct, you could use array_diff() to delete an element if you know what the elements are that you are deleting. I chose to use an array_filter() though because you can do conditional deleting. For example, you can delete it only if it’s not numeric or if it’s greater than 5 characters.

    You can’t perform conditional deletes using an array_diff() but it is a simple way to delete items you do know the value.. Thanks for another quick way to delete an item from an array in php.

  • Correct, you could use array_diff() to delete an element if you know what the elements are that you are deleting. I chose to use an array_filter() though because you can do conditional deleting. For example, you can delete it only if it’s not numeric or if it’s greater than 5 characters.

    You can’t perform conditional deletes using an array_diff() but it is a simple way to delete items you do know the value.. Thanks for another quick way to delete an item from an array in php.

  • As of yet, PHP does not provide an ‘official’ way of removing an array element which is not guaranteed to be at the start or end of the array.

    So, after a bit of searching around I found this novel way of removing an element from an array by its value(s) using PHPs array_diff function

  • As of yet, PHP does not provide an ‘official’ way of removing an array element which is not guaranteed to be at the start or end of the array.

    So, after a bit of searching around I found this novel way of removing an element from an array by its value(s) using PHPs array_diff function

  • As of yet, PHP does not provide an ‘official’ way of removing an array element which is not guaranteed to be at the start or end of the array.

    So, after a bit of searching around I found this novel way of removing an element from an array by its value(s) using PHPs array_diff function