【发布时间】:2018-12-08 11:07:33
【问题描述】:
我正在查看来自 answer 的这段代码:
$string = 'item1:item2:itemx';
$res = array();
$temp = &$nested_array;
foreach(explode(':', $string) as $key) {
$temp = &$temp[$key];
}
仅感谢$temp = &$temp[$key],结果:
$res = [
"item1" => [
"item2" => [
"itemx" => & null
]]]
我不明白为什么&temp[$key] 实例化关联[$key => null] 而$temp[$key] 没有。
我做了一些调试,对于第一个 $key (item1):
- $a = $temp[$key]:
- 给出“未定义索引”通知。
- 转储
$a返回null。 - 转储
$temp返回null。
- $a = &$temp[$key]:
- 不给“未定义索引”通知。
- 转储
$a返回null。 - 转储
$temp[$key]返回null。 - 转储
$temp返回:["item1" => & null]
这意味着 $temp = & $temp[$key] 也实例化 $temp[$key] 或等效地在它前面加上:
$temp = &temp[$key]; <=> $temp[$key] = null; $temp = &temp[$key];
我想了解这是否在 php 文档中的某处进行了解释(我搜索了但我没有找到任何东西)或者我是否遗漏了一些东西。
谢谢
【问题讨论】:
标签: php arrays indexing reference undefined