Best way to clear a PHP array's values

PhpArrays

Php Problem Overview


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;
}

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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.

Solution 4 - Php

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.

Solution 5 - Php

Isn't unset() good enough?

unset($array);

Solution 6 - Php

How about $array_name = array(); ?

Solution 7 - Php

Use array_splice to empty an array and retain the reference:

array_splice($myArray, 0);

Solution 8 - Php

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
?>

Solution 9 - Php

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 [].

Solution 10 - Php

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.

Solution 11 - Php

For PHP >= 5.4 use

$var = []; 

Not sure if it's faster than

$var = array();

but at least looks cleaner.

Solution 12 - Php

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);

Solution 13 - Php

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

Solution 14 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionel_pup_leView Question on Stackoverflow
Solution 1 - PhpEric HerlitzView Answer on Stackoverflow
Solution 2 - PhpmpenView Answer on Stackoverflow
Solution 3 - PhpWolfsblvtView Answer on Stackoverflow
Solution 4 - PhpMadara's GhostView Answer on Stackoverflow
Solution 5 - PhpJohn CondeView Answer on Stackoverflow
Solution 6 - PhpBibhas DebnathView Answer on Stackoverflow
Solution 7 - PhpAllan JardineView Answer on Stackoverflow
Solution 8 - PhpBlessingView Answer on Stackoverflow
Solution 9 - PhpPHP phpView Answer on Stackoverflow
Solution 10 - PhpBueck0815View Answer on Stackoverflow
Solution 11 - PhpluenibView Answer on Stackoverflow
Solution 12 - PhpWaqar AlamgirView Answer on Stackoverflow
Solution 13 - PhpCharlesView Answer on Stackoverflow
Solution 14 - PhpPaulView Answer on Stackoverflow