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.