如何访问多维关联数组的值

时间:2013-01-23 20:41:06

标签: php key associative-array

我有一个名为$scores的数组,它有一个名为'team'的索引,它本身就是一个索引为'w','l'和'd'的数组。如何访问'w','l'和'd'的值?

我试过了:

echo '<td>' . $scores['team']['w'] . '</td>';
echo '<td>' . $scores['team']['l'] . '</td>';
echo '<td>' . $scores['team']['d'] . '</td>';

echo '<td>' . array_values(array_keys($scores['team'], 'w')) . '</td>';
echo '<td>' . array_values(array_keys($scores['team'], 'l')) . '</td>';
echo '<td>' . array_values(array_keys($scores['team'], 'd')) . '</td>';

但这些都行不通。

声明数组:

$scores = array (
                            'team' => array (
                                                'w'     => 0
                                            ,   'l'     => 0
                                            ,   'd'     => 0
                                            )
                    );

的print_r($分数):

Array ( [team] => Array ( [w] => 0 [l] => 0 [d] => 0 ) [team one] => Array ( [l] => 2 [w] => 1 [d] => 1 ) [team two] => Array ( [w] => 1 [l] => 1 [d] => 1 ) [team three] => Array ( [l] => 1 ) [team four] => Array ( [w] => 1 ) [team five] => Array ( [w] => 1 ) )

1 个答案:

答案 0 :(得分:0)

现在已经解决了这个问题 - 感谢贡献者。

问题是我试图从for循环中访问该值:

for($i = 0; $i < count($teams); $i++)
{
    echo '<td>' . $scores[$teams[$i]]['l'] . '</td>';
}

这给了我正在寻找的答案。