Echo多维数组

时间:2016-06-03 20:21:48

标签: php arrays multidimensional-array

我的阵列:

$array = array('name'=>'test','server'=>'zangarmarsh','fields'=>'items,stats');
$type = 'character';
$r = $client->fetch($type,$array);
echo '<pre>';
print_r($r);
echo '</pre>';

我从数组中得到的输出:

 Array
    (
        [result] => Array
            (
                [lastModified] => 1464923915000

                [items] => Array
                    (
                        [averageItemLevel] => 710

                        [head] => Array
                            (
                                [id] => 125899

                                [tooltipParams] => Array
                                    (
                                        [transmogItem] => 71356

                                    )

                                [bonusLists] => Array
                                    (
                                    )        
                            )

                        [neck] => Array
                            (
                                [id] => 127976

                                [tooltipParams] => Array
                                    (

                                        [upgrade] => Array
                                            (

                                                [itemLevelIncrement] => 0
                                            )

                                        [timewalkerLevel] => 100
                                    )

我知道我需要使用foreach,但我有点负担过重。如果我使用这个,我得到[result]数组的输出,但是如何从其他数据中获取信息。

    foreach ($r as $v1) {
        foreach ($v1 as $v2) {
            echo "$v2\n";
        }
    }

2 个答案:

答案 0 :(得分:1)

这取决于你想要达到的目标。 例如,如果您始终具有相同的结果结构:使用条目&#34;结果&#34;在里面&#34;结果&#34; a&#34;修改&#34;条目和任意数量的项目,并且您想要访问可以执行以下操作的项目:

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

答案 1 :(得分:0)

另一种解决方案。下一个递归函数(search_keys)在多维数组中搜索两个键:array-key和item-key,如果两者都找到,则显示值:

<?php
$arr = Array
       ( "result" => Array
          ( "lastModified" => 1464923915000,
            "items" => Array
             ( "averageItemLevel" => 710,
               "head" => Array
                ( "id" => 125899,
                  "tooltipParams" => Array
                   ( "transmogItem" => 71356
                   ),
                  "bonusLists" => Array
                   (
                   )        
                ),
               "neck" => Array
                ( "id" => 127976,
                  "tooltipParams" => Array
                   ( "upgrade" => Array
                      ( "itemLevelIncrement" => 0
                      ),
                     "timewalkerLevel" => 100
                   )
                )
             )
          )
    );

search_keys( $arr,"head","id" );
search_keys( $arr,"tooltipParams","transmogItem" );
search_keys( $arr,"tooltipParams","timewalkerLevel" );

function search_keys ( $arr,$array_key,$item_key )
{ $keys = array_keys( $arr ); // GET ALL KEYS FROM CURRENT ARRAY.
  foreach ( $keys as $key )
    if ( gettype( $arr[ $key ] ) == "array" ) // IF CURRENT KEY IS ARRAY
       if ( ( $key == $array_key ) && // IF CURRENT KEY IS ARRAY_KEY, AND
            ( array_key_exists( $item_key,$arr[ $key ] ) ) ) // ARRAY CONTAINS ITEM
         echo $arr[ $key ][ $item_key ] . "<br/>"; // DISPLAY VALUE.
    else search_keys( $arr[ $key ],$array_key,$item_key ); // ENTER SUB-ARRAY.
}
?>