[php] Best way to clear a PHP array's values

Which is more efficient for clearing all values in an array? The first one would require me to use that function each time in the loop of the second example.

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

Or this

foreach($blah_blah as $blah) {
    $foo = array();
    //do something
    $foo = null;
}

This question is related to php arrays

The answer is


I see this questions is realla old, but for that problem I wrote a recursive function to unset all values in an array. Recursive because its possible that values from the given array are also an array. So that works for me:

function empty_array(& $complete_array) {
    foreach($complete_array as $ckey => $cvalue)
    {
        if (!is_array($cvalue)) {
            $complete_array[$ckey] = "";
        } else {
            empty_array( $complete_array[$ckey]);
        }

    }
    return $complete_array;

}

So with that i get the array with all keys and sub-arrays, but empty values.


Isn't unset() good enough?

unset($array);

Use array_splice to empty an array and retain the reference:

array_splice($myArray, 0);


i have used unset() to clear the array but i have come to realize that unset() will render the array null hence the need to re-declare the array like for example

<?php 
    $arr = array();
    array_push($arr , "foo");
    unset($arr); // this will set the array to null hence you need the line below or redeclaring it.
    $arr  = array();

    // do what ever you want here
?>

This is powerful and tested unset($gradearray);//re-set the array


The unset function is useful when the garbage collector is doing its rounds while not having a lunch break;

however unset function simply destroys the variable reference to the data, the data still exists in memory and PHP sees the memory as in use despite no longer having a pointer to it.

Solution: Assign null to your variables to clear the data, at least until the garbage collector gets a hold of it.

$var = null;

and then unset it in similar way!

unset($var);

For PHP >= 5.4 use

$var = []; 

Not sure if it's faster than

$var = array();

but at least looks cleaner.


The question is not really answered by the posts. Keeping the keys and clearing the values is the focus of the question.

foreach($resultMasterCleaned['header'] as $ekey => $eval) {
    $resultMasterCleaned[$key][$eval] = "";                 
}

As in the case of a two dimensional array holding CSV values and to blank out a particular row. Looping through seems the only way.


Maybe simple, economic way (less signs to use)...

$array = [];

We can read in php manual :

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].


Sadly I can't answer the other questions, don't have enough reputation, but I need to point something out that was VERY important for me, and I think it will help other people too.

Unsetting the variable is a nice way, unless you need the reference of the original array!

To make clear what I mean: If you have a function wich uses the reference of the array, for example a sorting function like

function special_sort_my_array(&$array)
{
    $temporary_list = create_assoziative_special_list_out_of_array($array);

    sort_my_list($temporary_list);

    unset($array);
    foreach($temporary_list as $k => $v)
    {
        $array[$k] = $v;
    }
}

it is not working! Be careful here, unset deletes the reference, so the variable $array is created again and filled correctly, but the values are not accessable from outside the function.

So if you have references, you need to use $array = array() instead of unset, even if it is less clean and understandable.


I'd say the first, if the array is associative. If not, use a for loop:

for ($i = 0; $i < count($array); $i++) { unset($array[$i]); }

Although if possible, using

$array = array();

To reset the array to an empty array is preferable.


How about $array_name = array(); ?


If you just want to reset a variable to an empty array, you can simply reinitialize it:

$foo = array();

Note that this will maintain any references to it:

$foo = array(1,2,3);
$bar = &$foo;
// ...
$foo = array(); // clear array
var_dump($bar); // array(0) { } -- bar was cleared too!

If you want to break any references to it, unset it first:

$foo = array(1,2,3);
$bar = &$foo;
// ...
unset($foo); // break references
$foo = array(); // re-initialize to empty array
var_dump($bar); // array(3) { 1, 2, 3 } -- $bar is unchanged