从多维数组中回显

时间:2016-06-05 12:24:45

标签: php arrays multidimensional-array echo

我有这个数组:

Array
(
    [result] => Array
        (
            [lastModified] => 1465097340000
            [name] => Ulminia
            [realm] => Zangarmarsh
            [battlegroup] => Rampage
            [class] => 3
            [race] => 4
            [gender] => 1
            [level] => 100
            [achievementPoints] => 14915
            [thumbnail] => hellscream/74/113337162-avatar.jpg
            [calcClass] => Y
            [faction] => 0
            [items] => Array
                (
                    [averageItemLevel] => 710
                    [averageItemLevelEquipped] => 709
                    [head] => Array
                        (
                            [id] => 125899
                            [name] => Warmongering Gladiator's Helm
                            [icon] => inv_helm_mail_raidhunter_p_01
                            [quality] => 4
                            [itemLevel] => 710
                            [tooltipParams] => Array
                                (
                                    [transmogItem] => 71356
                                    [timewalkerLevel] => 100
                                )

我想从[head]数组回显[id]和[quality]。如果我只是回应[id]一切正常,但如果我想要回应[质量],它也行不通。

我的代码:

$items = $r['result']['items'];
echo 'Head: '.$items['head']['id']['quality']."\n";
foreach($items['head']['tooltipParams'] as $key => $value){
    echo 'head_'.$key.': '.$value.'\n';

}

3 个答案:

答案 0 :(得分:1)

echo $items['head']['id']['quality'];

上述声明表示您正在打印子键" quality"密钥" id",它不存在。

您需要按如下方式连接两个键值:

echo $items['head']['id'] . ' ' . $items['head']['quality'];

......或

echo $items['head']['id'], ' ', $items['head']['quality'];

答案 1 :(得分:0)

请注意,idquality位于同一个数组中。

//ID
echo $items['head']['id'];
//Quality
echo $items['head']['quality'];

答案 2 :(得分:0)

$items['head']['id']['quality']."\n";正在尝试使用quality中存储的数组中的键id 来读取元素。但是,id不是数组,这就是失败的原因。

为了阅读两个字段,您需要单独阅读它们:

echo 'Head: ID=' . $items['head']['id'] . ', quality = ' . $items['head']['quality'] . "\n";