这个关联数组如何工作?

时间:2016-07-11 18:22:50

标签: php associative-array

我很困惑我的服务器上的错误如何显示错误报告最多?欢迎任何见解。

$myArray = ['first' => '1A', 'second' => '2A', 'first' => '2A', 'second' => '2B'];

foreach($myArray as $value) {
    echo $value['first'] . "<br />";
}

输出:

1A
2A

2 个答案:

答案 0 :(得分:3)

您有重复的数组键,但阵列上不允许这样做检查Arrays

你必须重新格式化你的数组

$myArray = [['first' => '1A', 'second' => '2A'], ['first' => '2A', 'second' => '2B']];
foreach($myArray as $key => $value) {
    echo $value['first']."<br / >";
}

答案 1 :(得分:1)

First, You have duplicated the Keys in the array and that is not allowed. If you want make the array with the key, you should use something like this:

  foreach (array_expression as $key => $value)

http://php.net/manual/en/control-structures.foreach.php