【发布时间】:2021-02-22 11:27:09
【问题描述】:
我有这个生成多维关联数组的 php 脚本:
<?php
$a=array();
$x = 0;
while($x < 2)
{
$a["color"] = array();
if($x == 0)
{
$a["color"]["price"] = "25";
}
else
{
$a["color"]["price"] .= "5";
}
$x += 1;
}
print_r($a);
?>
我总是收到Notice: Undefined index: price。
我已经在 if 语句中明确定义了price 索引,那为什么会这样呢?
【问题讨论】:
-
$a["color"]["price"] .= "5";你正在连接到$a["color"]["price"],它还不存在/不再存在 -
亲爱的 closevoter 使用错字对齐,OP 没有在键盘上滑动并意外输入 22 个字符:
$a["color"] = array();
标签: php arrays loops multidimensional-array concatenation