简体   繁体   English

清空数组的最有效方法

[英]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(); 如果没有那么$ 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. PHP适用于变量的“引用”。 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. PHP 垃圾收集器可以提供有关此主题的更多信息。

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. 我注意到在关于在每个键上使用unset()的注释中进行了更长时间的讨论。

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. 当对特定数据结构的引用达到0时,将从内存中清除它。 Both =array() and unset will do the same thing. 两个=array()unset都会做同样的事情。

Now if you don't actually need array() you can use null : 现在,如果您实际上不需要array() ,则可以使用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. $ array将是现有的和数组类型(但是为空),您的数据可以稍后从内存中获取。

Well... why not: $array = array(); 那么......为什么不呢: $array = array(); ?

As Suresh Kamrushi pointed out, I could use array_keys: 正如Suresh Kamrushi指出的那样,我可以使用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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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