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:
unset($products['XL1000']);
However, if it 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[1]; // prints 'bee'
print $animals[2]; // prints 'cat'
count($animals); // returns 6
// unset( )
unset($animals[1]); // removes element $animals[1] = 'bee'
print $animals[1]; // prints '' and throws an E_NOTICE error
print $animals[2]; // still prints 'cat'
count($animals); // returns 5, even though $array[5] is 'fox'
// add new element
$animals[ ] = 'gnu'; // add new element (not Unix)
print $animals[1]; // prints '', still empty
print $animals[6]; // prints 'gnu', this is where 'gnu' ended up
count($animals); // returns 6
// assign ''
$animals[2] = ''; // zero out value
print $animals[2]; // prints ''
count($animals); // returns 6, count does not decrease