Do I really have to do this to reset an array?
foreach ($array as $i => $value) { unset($array[$i]); }
EDIT:
This one makes more sense, as the previous one is equivalent to $array=array();
foreach ($array as $i => $value) { $array[$i]=NULL; }
$keys = array_keys($array); $values = array_fill(0, count($keys), null); $new_array = array_combine($keys, $values);
Get the Keys
Get an array of nulls with the same number of elements
Combine them, using keys and the keys, and the nulls as the values
As comments suggest, this is easy as of PHP 5.2 with array_fill_keys
$new_array = array_fill_keys(array_keys($array), null);
Fill array with old keys and null values
$array = array_fill_keys(array_keys($array), null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With