Jan. 24, 2011, 8:12 a.m.
posted by stackme
Deleting Elements from an Array
To delete multiple noncontiguous elements, also use unset( ):
unset($array, $array); unset($array['foo'], $array['bar']);
array_splice($array, $offset, $length);
Using these functions removes all references to these elements from PHP. If you want to keep a key in the array, but with an empty value, assign the empty string to the element:
$array = $array['foo'] = '';
Besides syntax, there's a logical difference between using unset( ) and assigning '' to the element. The first says, "This doesn't exist anymore," while the second says, "This still exists, but its value is the empty string."
If you're dealing with numbers, assigning 0 may be a better alternative. So if a company stopped production of the model XL1000 sprocket, it would update its inventory with:
However, if the company temporarily ran out of XL1000 sprockets but was planning to receive a new shipment from the plant later this week, this is better:
$products['XL1000'] = 0;
If you unset( ) an element, PHP adjusts the array so that looping still works correctly. It doesn't compact the array to fill in the missing holes. This is what we mean when we say that all arrays are associative, even when they appear to be numeric. Here's an example:
// create a "numeric" array $animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox'); print $animals; // prints 'bee' print $animals; // prints 'cat' count($animals); // returns 6 // unset() unset($animals); // removes element $animals = 'bee' print $animals; // prints '' and throws an E_NOTICE error print $animals; // still prints 'cat' count($animals); // returns 5, even though $array is 'fox' // add new element $animals = 'gnu'; // add new element (not Unix) print $animals; // prints '', still empty print $animals; // prints 'gnu', this is where 'gnu' ended up count($animals); // returns 6 // assign '' $animals = ''; // zero out value print $animals; // prints '' count($animals); // returns 6, count does not decrease
To compact the array into a densely filled numeric array, use array_values( ):
$animals = array_values($animals);
Alternatively, array_splice( ) automatically reindexes arrays to avoid leaving holes:
// create a "numeric" array $animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox'); array_splice($animals, 2, 2); print_r($animals); Array (  => ant  => bee  => elk  => fox )
This is useful if you're using the array as a queue and want to remove items from the queue while still allowing random access. To safely remove the first or last element from an array, use array_shift( ) and array_pop( ), respectively.
However, if you find yourself often running into problems because of holes in arrays, you may not be "thinking PHP." Look at the ways to iterate through the array in Recipe 4.4 that don't involve using a for loop.
Recipe 4.4 for iteration techniques; documentation on unset( ) at http://www.php.net/unset, array_splice( ) at http://www.php.net/array-splice, and array_values( ) at http://www.php.net/array-values .