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;
}
10 个解决方案
#1
184
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
#2
65
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
#3
26
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.
这不是工作!在这里要小心,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,即使它不那么干净和可以理解。
#4
11
I'd say the first, if the array is associative. If not, use a for
loop:
首先,如果数组是关联的。如果没有,请使用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.
将数组重置为空数组更可取。
#6
6
How about $array_name = array();
?
$array_name =数组();吗?
#7
4
Use array_splice
to empty an array and retain the reference:
使用array_splice清空一个数组并保留引用:
array_splice($myArray, 0);
作用(myArray美元,0);
#8
0
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.
然而,unset函数只是破坏了对数据的变量引用,数据仍然存在于内存中,而PHP则将内存视为正在使用的内存,尽管不再有指向它的指针。
Solution: Assign null
to your variables to clear the data, at least until the garbage collector gets a hold of it.
解决方案:将null赋值给变量以清除数据,至少在垃圾收集器得到它之前。
$var = null;
and then unset it in similar way!
然后以同样的方式取消它!
unset($var);
#9
0
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()将使数组为null,因此需要重新声明数组。
<?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
//做你想做的事。
?>
#10
-1
This is powerful and tested unset($gradearray);//re-set the array
这是强大的测试unset($gradearray);//重新设置数组。
#1
184
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
#2
65
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
#3
26
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.
这不是工作!在这里要小心,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,即使它不那么干净和可以理解。
#4
11
I'd say the first, if the array is associative. If not, use a for
loop:
首先,如果数组是关联的。如果没有,请使用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.
将数组重置为空数组更可取。
#5
#6
6
How about $array_name = array();
?
$array_name =数组();吗?
#7
4
Use array_splice
to empty an array and retain the reference:
使用array_splice清空一个数组并保留引用:
array_splice($myArray, 0);
作用(myArray美元,0);
#8
0
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.
然而,unset函数只是破坏了对数据的变量引用,数据仍然存在于内存中,而PHP则将内存视为正在使用的内存,尽管不再有指向它的指针。
Solution: Assign null
to your variables to clear the data, at least until the garbage collector gets a hold of it.
解决方案:将null赋值给变量以清除数据,至少在垃圾收集器得到它之前。
$var = null;
and then unset it in similar way!
然后以同样的方式取消它!
unset($var);
#9
0
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()将使数组为null,因此需要重新声明数组。
<?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
//做你想做的事。
?>
#10
-1
This is powerful and tested unset($gradearray);//re-set the array
这是强大的测试unset($gradearray);//重新设置数组。