清除 PHP 数组值的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10261925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to clear a PHP array's values
提问by el_pup_le
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;
}
回答by Eric Herlitz
Like Zack said in the comments below you are able to simply re-instantiate it using
就像 Zack 在下面的评论中所说的那样,您可以简单地使用重新实例化它
$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 ,如果您稍后需要该数组,只需再次实例化它。
unset($foo); // $foo is gone
$foo = array(); // $foo is here again
回答by mpen
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
回答by Wolfsblvt
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, unsetdeletes the reference, so the variable $arrayis created again and filled correctly, but the values are not accessable from outside the function.
它不工作!这里要小心,unset删除引用,所以变量$array被再次创建并正确填充,但值不能从函数外部访问。
So if you have references, you need to use $array = array()instead of unset, even if it is less clean and understandable.
因此,如果您有引用,则需要使用$array = array()而不是unset,即使它不太清晰和易于理解。
回答by Madara's Ghost
I'd say the first, if the array is associative. If not, use a forloop:
如果数组是关联的,我会说第一个。如果没有,请使用for循环:
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.
最好将数组重置为空数组。
回答by Bibhas Debnath
How about $array_name = array();?
怎么样$array_name = array();?
回答by Allan Jardine
Use array_spliceto empty an array and retain the reference:
使用array_splice空的数组,并保留参考:
array_splice($myArray, 0);
array_splice($myArray, 0);
回答by Blessing
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
我已经使用 unset() 来清除数组,但我已经意识到 unset() 将使数组为空,因此需要重新声明数组,例如
<?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
?>
回答by Bueck0815
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.
因此,我得到了包含所有键和子数组的数组,但值为空。
回答by PHP php
Maybe simple, economic way (less signs to use)...
也许简单,经济的方式(使用较少的标志)......
$array = [];
We can read in php manual :
我们可以在php手册中阅读:
As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
从 PHP 5.4 开始,您还可以使用短数组语法,它将 array() 替换为 []。

