为什么不能我把元素添加到在foreach循环子阵? [英] Why cant i push element onto sub array in foreach loop?
问题描述
我想了解一些有关在每个循环中的数组可能是明显的多。
I'm trying to understand something about arrays in for each loops that might be obvious to many.
当通过我的多维数组i循环,我试图找到没有第三元件子阵列。如果他们没有第三元件,我要到第三元素添加到该子阵列与一个特定的值。
When i loop through my multi-dimensional array, i attempt to find sub arrays with no third element. If they have no third element, i want to add a third element to that sub array with a specific value.
$testArray = array (
array("Green", "Yellow", "Blue"),
array("Brown", "Silver"),
array("Orange", "Pink", "Black"),
);
当我使用foreach循环:
When i use the foreach loop:
foreach ( $testArray as $key => $array ) {
if (count($array) == '2') {
$array[] = "None";
};
}
没有错误抛出,但没有任何反应。当我使用的每一个循环:
No errors are thrown but nothing happens. When i use the for each loop:
foreach ( $testArray as $key => $array ) {
if (count($array) == '2') {
$testArray[$key][] = "None";
};
}
它按预期工作。
很抱歉的长期preamble,我的问题是:
Sorry for the long preamble, my questions is:
为什么没有这两个foreach循环做同样的事情?谢谢!
Why aren't these two foreach loops doing the same thing? Thanks!
推荐答案
这里的问题在于,超过iterables的foreach迭代和值设置迭代变量。这意味着 $阵列
您要在处理的foreach
不是<$ C的值相同$ C> $ testArray 。
The problem here lies in the fact that foreach iterates over iterables and sets the iteration variable by value. This means that the $array
which you are dealing with in the foreach
is not the same value of the $testArray
.
要rememdy这(和避免引入一个 $指数
变量变异数组中的一个项目),你需要告诉的foreach来的通过引用传递中的价值。引用是PHP的答案C风格的指针。如果一个变量引用另一个,这两个变量指向相同的值,因此一个的内容的修改是有效的其他的变形例。在你的的foreach
,您可以用&放大器; $阵列
有循环传给你<$ C $的项目C> $ testArray :
To rememdy this (and avoid introducing an $index
variable to mutate an item in the array), you will need to tell foreach to pass the value by reference. References are PHP's answer to C-style pointers. If a variable references another, both variables point to the same value, so a modification to the contents of one is in effect a modification to the other. In your foreach
, you can use &$array
to have the loop pass you the items of $testArray
by reference instead of by value:
foreach ( $testArray as $key => &$array ) {
if (count($array) == '2') {
$array[] = "None";
}
}
( codePAD演示)
这对齐与PHP的引用,在哪里可以取得一个变量引用另一个像这样:
This aligns with PHP's references, where one variable can be made to reference another like so:
$a = array(1, 2, 3);
$b = &$a;
$b[] = 4;
print_r($a); // 1, 2, 3, 4
( codePAD演示)
您遇到类似现象与功能:
You experience a similar phenomenon with functions:
function byValue($a) {
$a[] = 4;
}
function byRef(&$a) {
$a[] = 5;
}
$a = array(1, 2, 3);
byValue($a);
print_r($a); // 1, 2, 3
byRef($a);
print_r($a); // 1, 2, 3, 5
( codePAD演示)
借助引用文档PHP的部分有对此问题的一些例子的foreach
的语法。还要注意这个(有点)有关,但在的foreach
有趣的阅读和引用的。
The references section of the PHP docs has some examples about this syntax of foreach
. Also note this (somewhat) related, but interesting read on foreach
and references.
此外,在一个不相关的音符,如果你不知道:你不需要关闭块后用分号}
在PHP中,除非你正在做的事就像给一个变量分配一个封闭:
Also, on an unrelated note if you weren't aware: you don't need a semicolon after closing a block with }
in PHP unless you're doing something like assigning a closure to a variable:
$someFunc = function($a) { return $a; }; //need one here
if(1 + 2 == 4) {
echo "I just broke math";
} // but you don't need one here
这篇关于为什么不能我把元素添加到在foreach循环子阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!