可能重复:
参考 - PHP 中这个符号的含义是什么?
我需要知道为什么在 foreach 循环中要在变量前使用 ampersand 符号。
foreach ($wishdets as $wishes => &$wishesarray) {
foreach ($wishesarray as $categories => &$categoriesarray) {
}
}
可能重复:
参考 - PHP 中这个符号的含义是什么?
我需要知道为什么在 foreach 循环中要在变量前使用 ampersand 符号。
foreach ($wishdets as $wishes => &$wishesarray) {
foreach ($wishesarray as $categories => &$categoriesarray) {
}
}
这个示例将向你展示它们之间的区别。
$array = array(1, 2);
foreach ($array as $value) {
$value++;
}
print_r($array); // 1, 2 because we iterated over copy of value
foreach ($array as &$value) {
$value++;
}
print_r($array); // 2, 3 because we iterated over references to actual values of array
请查阅此处的PHP文档:http://pl.php.net/manual/en/control-structures.foreach.php
它用于将数组的单个实例中的更改应用到主数组中。
例如:
//现在更改不会影响数组 $wishesarray
foreach ($wishesarray as $id => $categoriy) {
$categoriy++;
}
print_r($wishesarray); //It'll same as before..
foreach ($wishesarray as $id => &$categoriy) {
$categoriy++;
}
print_r($wishesarray); //It'll have values all increased by one..
针对您问题中的代码,无法给出具体的答案,因为内部的foreach循环为空。
从您的代码中看到,内部的foreach
是在引用上进行迭代,而不是常规的方式。
我建议您阅读foreach
PHP 手册页面,它涵盖了所有四种情况:
foreach($standard as $each);
foreach($standard as &$each); # this is used in your question
$reference = &$standard;
foreach($reference as $each);
$reference = &$standard;
foreach($reference as &$each); # this is used in your question