Efficiency of using foreach loops to clear a PHP array's values
arrays, php
Solution
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 we are talking about very large tables I'd probably recommend
$foo = null;
unset($foo);
since that also would clear the memory a bit better. That behavior (GC) is however not very constant and may change over PHP versions. Bear in mind that re-instantiating a structure is not the same as emptying it.
Problem
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; } ``` I don't want to use `unset()` because that deletes the variable.