Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to clear a PHP array's values

Tags:

arrays

php

People also ask

How do you clear an array variable in PHP?

Use the unset() Function to Reset an Array in PHP We will use the unset() function to clear array values. The unset() functions resets a variable.

How do you clear a function in PHP?

The Ds\Stack::clear() function of PHP is used to remove all elements from a Stack and clear it. This function simply removes all of the elements from the Stack but not completely deletes it.

How do you unset an associative array in PHP?

Answer: Use the PHP unset() Function If you want to delete an element from an array you can simply use the unset() function. The following example shows how to delete an element from an associative array and numeric array.

What does Array_splice () function do?

The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).


Like Zack said in the comments below you are able to simply re-instantiate it using

$foo = array(); // $foo is still here

If you want something more powerful use unset since it also will clear $foo from the symbol table, if you need the array later on just instantiate it again.

unset($foo); // $foo is gone
$foo = array(); // $foo is here again

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

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.


Isn't unset() good enough?

unset($array);

How about $array_name = array(); ?


Use array_splice to empty an array and retain the reference:

array_splice($myArray, 0);