delete all values from an array while keeping keys intact

Viewed 28699

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;
}
13 Answers
$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);

There is no build-in function to reset an array to just it's keys.

An alternative would be via a callback and array_map():

$array = array( 'a' => 'foo', 'b' => 'bar', 'c' => 'baz' );

With regular callback function

function nullify() {}
$array = array_map('nullify', $array);

Or with a lambda with PHP < 5.3

$array = array_map(create_function('', ''), $array);

Or with lambda as of PHP 5.3

$array = array_map(function() {}, $array);

In all cases var_dump($array); outputs:

array(3) {
  ["a"]=> NULL
  ["b"]=> NULL
  ["c"]=> NULL
}

Define this function and call it whenever you need it:

function erase_val(&$myarr) {
    $myarr = array_map(create_function('$n', 'return null;'), $myarr);
}

// It's call by reference so you don't need to assign your array to a variable.
// Just call the function upon it
erase_val($array);

That's all!

Get the array keys, then use them to create a new array with NULL values:

array_fill_keys(array_keys($array), NULL);

About array_fill_keys():

The array_fill_keys() function fills an array with values, specifying keys.

About array_keys():

The array_keys() function returns all the keys of an array.

foreach($a as &$v)
   $v = null;

The reasoning behind setting an array item to null is that an array needs to have a value for each key, otherwise a key makes no sense. That is why it is called a key - it is used to access a value. A null value seems like a reasonable choice here.

Wrap it in a [reusable] procedure:

function array_purge_values(&$a) {
    foreach($a as &$v)
       $v = null;
}

Keep in mind though that PHP versions 5.3 and those released later, pass values to functions by reference by default, i.e. the ampersand preceding argument variable in the function declaration is redundant. Not only that, but you will get a warning that the notion is deprecated.

unset would delete the key, You need to set the value to null or 0 as per your requirement.

Example

I don't get the question quite well, but your example

foreach ($array as $i => $value) {
    unset($array[$i]);
}

is equivilent to

$array = array();

Why not making an array with required keys and asinging it to variable when you want reset it?

function resetMyArr(&$arr)
{
 $arr = array('key1'=>null,'key2'=>null); 
}

And speed test This test shows speed when clearing a large array

$a1 = array();
for($i=0;$i<=1000;$i++)
{
    $a1['a'.$i] = $i;
}
$b = $a1;
$start_time = microtime(TRUE);
    foreach ($a1 as $field => $val) {
      $a1[$field]=NULL;
    } 
$end_time = microtime(TRUE);
$duration = $end_time - $start_time;
var_dump( $duration*1000 );

var_dump('all emelent of array is '.reset($a1));


$start_time = microtime(TRUE);
$allkeys = array_keys($b);
$newarray = array_fill_keys($allkeys, null);

$end_time = microtime(TRUE);
$duration = $end_time - $start_time;
var_dump( $duration*1000 );

var_dump('all emelent of array is '.reset($newarray));
Related