在一个阵列中清除所有价值的效率如何? 第一个例子要求我每次在第二个例子的空档中使用这一功能。
foreach ($array as $i => $value) {
unset($array[$i]);
}
或者
foreach($blah_blah as $blah) {
$foo = array();
//do something
$foo = null;
}
我不想使用<代码>unset(),因为删除了变量。
在一个阵列中清除所有价值的效率如何? 第一个例子要求我每次在第二个例子的空档中使用这一功能。
foreach ($array as $i => $value) {
unset($array[$i]);
}
或者
foreach($blah_blah as $blah) {
$foo = array();
//do something
$foo = null;
}
我不想使用<代码>unset(),因为删除了变量。
同Zack在以下评论中说过一样,你能够简单地利用它来进行反证。
$foo = array(); // $foo is still here
如果你想要更强有力的使用,因为如果你需要紧随时间,那么,你也会从象征桌上清除烟.。
unset($foo); // $foo is gone
$foo = array(); // $foo is here again
如果我们谈论的是非常大的表格,我可能建议
$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.
If you just want to reset a variable to an empty array, you can simply reinitialize it:
$foo = array();
注
$foo = array(1,2,3);
$bar = &$foo;
// ...
$foo = array(); // clear array
var_dump($bar); // array(0) { } -- bar was cleared too!
如果你想打破对它的任何提及,首先要问:
$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
如果你需要原始阵列的提及,那么确定变量是一种冰 way,
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;
}
}
它没有工作! 此处仔细看,<代码>unset删除了这一提法,因此,可变的<代码>$array再次创建并正确填满,但数值不能从职能外取用。
因此,如果你有参考,你需要使用<条码>$array = 阵列()而不是<条码>unset,即使该编码不干净,也难以理解。
I d say the first, if the array is associative. If not, use a for
loop:
for ($i = 0; $i < count($array); $i++) { unset($array[$i]); }
尽可能使用
$array = array();
最好将阵列重新定位为空阵。
Isn t unset(
够好吗?
unset($array);
How about $array_name = array();
?
Use array_splice
to empty an array and retain the reference:
array_splice($myArray, 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
<?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
?>
也许简单、经济的方式(没有迹象可使用)。
$array = [];
We can read in php manual :
到PHP5.4为止,你还可以使用短阵列合成物,用[]取代阵列。
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;
}
这样一来,就有了所有关键和次级分支的阵列,而是空的价值观。
The unset function is useful when the garbage collector is doing its rounds while not having a lunch break;
然而,不定的职能只是销毁了数据的不同参考资料,而数据仍存在于记忆中,而PHP认为,尽管不再有数据点,但记忆在使用中。
Solution:
Assign null
to your variables to clear the data, at least until the garbage collector gets a hold of it.
$var = null;
而随后又以类似方式加以 un弄!
unset($var);
员额没有真正回答这个问题。 保持关键因素和清理价值观是问题的重点。
foreach($resultMasterCleaned[ header ] as $ekey => $eval) {
$resultMasterCleaned[$key][$eval] = "";
}
如同两种维权阵列一样,持有特别志愿人员的价值观,并row开一行。 选择途径似乎是唯一的途径。
[] is nearly 30% faster then as array() similar as pushing new element to array $array[] = $newElement then array_push($array, $newElement) (keep in mind array_push is slower only for single or 2 new elements)
Reason is we skip overhead function to call those
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...