简体   繁体   中英

Most efficient way to empty an array

I have an array containing several keys, values, objects etc.. I need to empty that array but I'd like to do it in the most efficient manner.

The best I can come up with is:

foreach ($array as $key => $val) unset($array[$key]);

But I don't like the idea of having to loop through the array to just empty it.. surely there's a nice slick/clever way of doing this without wasting memory creating a new array?

Note: I'm not sure myself if it does cost extra memory in creating the array as new again. If it doesn't then $array = new array(); would be a fine way of 'emptying' it.

试试:

$array = array();

It highly depends on what you mean.

To empty the current reference you can always do

$array = array();

To completely remove the current instance from the scope

unset($array);

Unfortunately both of these cases don't necessarily mean the memory associated with each element is released.

PHP works with something called "references" for your variables. Your variables are actually labels or references pointing to data, not the actual container for data. The PHP garbage collector can offer more insight on this subject.

Now take a look at this example , taken from the docs:

$a = "new string";
$c = $b = $a;
xdebug_debug_zval( 'a' );# a: (refcount=3, is_ref=0)='new string'
unset( $b, $c );
xdebug_debug_zval( 'a' );# a: (refcount=1, is_ref=0)='new string'

This unfortunately applies to all your variables. Including arrays. Cleaning up the memory associated with the array is a whole different subject I'm afraid.


I've noticed a longer discussion in the comments regarding using unset() on each individual key.

This feels like extremely bad practice. Consider the following code:

class A{
    function __construct($name){$this->name=$name;}
    function __destruct(){echo $this->name;}
}

$a=array();
$b=array();
$c=array();
for($i=0;$i<5;$i++) {
    $a[]=new A('a');
    $b[]=new A('b');
    $c[]=new A('c');
}

unset($a);
$b=array();

echo PHP_EOL.'done'.PHP_EOL;

This will output:

aaaaabbbbb
done
ccccc

When the reference to a particular data structure reaches 0, it is cleaned from memory. Both =array() and unset will do the same thing.

Now if you don't actually need array() you can use null :

$array=null;

This keeps the label in memory, but removes the reference it held to any particular data.

It's simple:

$array = array();

$array will be existing and type of array (but empty), and your data can be garbaged later from memory.

Well... why not: $array = array(); ?

As Suresh Kamrushi pointed out, I could use array_keys:

foreach (array_keys($array) as $key) unset($array[$key]);

This is probably the nicest solution for now.. but I'm sure someone will come up with something better soon :)

Try this:

// $array is your original array

$array = array_combine( array_keys( $array ), array_fill( 0, count($array), 0 ) );

The above will blank your array keeping the keys intact.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM