PHP多维数组元素被跳过
我已经设置了一个多维阵列中PHP是这样的:PHP多维数组元素被跳过
$contents = array(
"Header1" => array(
"Section 1" => array (
"Description1",
"Notes1",
),
"Gap" => "Gap",
"Section 2" => array (
"Description2",
"Notes2",
),
"Gap" => "Gap",
"Section 3" => array (
"Description3",
"Notes3",
),
),
);
然后我通过这个阵列循环如下:
foreach ($contents as $header => $section) {
foreach ($section as $title => $details) {
echo $title."<br>";
}
}
的输出将是:
Section1
Gap
Section2
Section3
为什么不是第二个“差距”显示?
Thanx
因为你不能有重复的数组键。第二个覆盖第一个。
使用Gap2
或您的下一个数组键。或者,更好的是,嵌套它:
array(
'Gap' => array(
'Gap1',
'Gap2'
)
);
第一个覆盖第二个:) – 2013-02-20 20:56:05
@Akam奥利? http://codepad.org/RyRO94MT – 2013-02-20 20:57:41
请参阅http://phpfiddle.org/main/code/x5y-g09 – 2013-02-20 21:01:35
你不能在你的数组中绑定到同一个键上的两个值。常用的方法是将多个值放入另一个子数组中,但使用相同的密钥。但它增加了代码的复杂性。小心。
这就是为什么:http://codepad.org/zvuYLP6P。 – 2013-02-20 20:54:24