PHP Unset Array 值对其他索引的影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6376702/
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
PHP Unset Array value effect on other indexes
提问by Oliver Spryn
I am working with a PHP loop, and I had one question regarding how unset affects the array keys. This array uses the standard numeric keys assigned by PHP, 0, 1, 2, 3 etc...
. Whenever unset()
runs on an array value, are the array keys shuffled or are they maintained as before?
我正在使用 PHP 循环,我有一个关于未设置如何影响数组键的问题。该数组使用 PHP 分配的标准数字键,0, 1, 2, 3 etc...
. 每当unset()
在数组值上运行时,数组键是混洗还是像以前一样维护?
Thank you for your time.
感谢您的时间。
回答by Michael Berkowski
The keys are not shuffled or renumbered. The unset()
key is simply removed and the others remain.
键不会被打乱或重新编号。该unset()
密钥仅删除和其他人保持。
$a = array(1,2,3,4,5);
unset($a[2]);
print_r($a);
Array
(
[0] => 1
[1] => 2
[3] => 4
[4] => 5
)
回答by Glen Solsberry
Test it yourself, but here's the output.
自己测试一下,但这是输出。
php -r '$a=array("a","b","c"); print_r($a); unset($a[1]); print_r($a);'
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => a
[2] => c
)
回答by Jeff Lambert
The Key Disappears, whether it is numeric or not. Try out the test script below.
密钥消失,无论是数字还是非数字。试试下面的测试脚本。
<?php
$t = array( 'a', 'b', 'c', 'd' );
foreach($t as $k => $v)
echo($k . ": " . $v . "<br/>");
// Output: 0: a, 1: b, 2: c, 3: d
unset($t[1]);
foreach($t as $k => $v)
echo($k . ": " . $v . "<br/>");
// Output: 0: a, 2: c, 3: d
?>
回答by genesis
They are as they were. That one key is JUST DELETED
他们和以前一样。那个键被删除了
回答by David Lartey
This might be a little bit out of context but in unsetting values from a global array, apply the answer by Michael Berkowski above but in use with $GLOBALS
instead of the the global value you declared with global $variable_name
. So it will be something like:
这可能有点脱离上下文,但在取消全局数组中的值时,请应用上面 Michael Berkowski 的答案,但使用 with$GLOBALS
而不是您声明的全局值global $variable_name
。所以它会是这样的:
unset($GLOBALS['variable_name']['array_key']);
unset($GLOBALS['variable_name']['array_key']);
Instead of:
代替:
global $variable_name;
unset($variable_name['array_key']);
global $variable_name;
unset($variable_name['array_key']);
NB: This works only if you're using global variables.
注意:这仅在您使用全局变量时才有效。
回答by Amr Berag
The keys are maintained with the removed key missing but they can be rearranged by doing this:
这些键在删除的键丢失的情况下保持不变,但可以通过执行以下操作重新排列它们:
$array = array(1,2,3,4,5);
unset($array[2]);
$arranged = array_values($array);
print_r($arranged);
Outputs:
输出:
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
Notice that if we do the following without rearranging:
请注意,如果我们在不重新排列的情况下执行以下操作:
unset($array[2]);
$array[]=3;
The index of the value 3 will be 5 because it will be pushed to the end of the array and will not try to check or replace missing index. This is important to remember when using FOR LOOP with index access.
值 3 的索引将为 5,因为它将被推送到数组的末尾并且不会尝试检查或替换丢失的索引。在使用 FOR LOOP 进行索引访问时,记住这一点很重要。