如何以这种格式显示此json数据?

时间:2015-08-14 14:31:14

标签: php json

我有以这种格式的json数据:

Array
(
    [type] => champion
    [version] => 5.15.1
    [data] => Array
        (
            [Thresh] => Array
                (
                    [id] => 412
                    [key] => Thresh
                    [name] => Thresh
                    [title] => the Chain Warden
                )

            [Aatrox] => Array
                (
                    [id] => 266
                    [key] => Aatrox
                    [name] => Aatrox
                    [title] => the Darkin Blade
                )

            [Tryndamere] => Array
                (
                    [id] => 23
                    [key] => Tryndamere
                    [name] => Tryndamere
                    [title] => the Barbarian King
                )

         )

)

如何在带有foreach循环的php中以下列格式显示此数据?

Name : Thresh
id : 412
key : Thresh
name : Thresh
title : the Chain Warden
------------------------------
Name : Aatrox
id : 266
key : Aatrox
name : Aatrox
title : the Darkin Blade
---------------------------------
Name : Tryndamere
id : 23
key : Tryndamere
name : Tryndamere
title : the Barbarian King
----------------------------------------------

1 个答案:

答案 0 :(得分:0)

迭代$arr['data']并打印结果:

foreach ($arr['data'] as $name => $data) {
    echo 'Name:' . $name . PHP_EOL;
    echo 'id:' . $data['id'] . PHP_EOL;
    echo 'key:' . $data['key'] . PHP_EOL;
    echo 'name:' . $data['name'] . PHP_EOL;
    echo 'title:' . $data['title'] . PHP_EOL;
    echo '------------------------------' . PHP_EOL;
}