循环数组以API格式显示或回显它

时间:2018-03-12 08:57:40

标签: javascript php json

[0] => Array

    (
        [id] => 6
        [name] => Digitally Imported Psy  Goatrance
        [country] => GB
        [image] => Array
            (
                [url] => https://img.dirble.com/station/6/original.png
                [thumb] => Array
                    (
                        [url] => https://img.dirble.com/station/6/thumb_original.png
                    )

            )

        [slug] => digitally-imported-psy-goatrance
        [website] => http://www.di.fm/
        [twitter] => 
        [facebook] => 
        [total_listeners] => 0
        [categories] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [title] => Trance
                        [description] => stations that plays commercial and other things in trance-music genre.
                        [slug] => trance
                        [ancestry] => 14
                    )

            )

        [streams] => Array
            (
                [0] => Array
                    (
                        [stream] => http://pub1.di.fm:80/di_goapsy
                        [bitrate] => 48000
                        [content_type] => audio/mpeg
                        [status] => 1
                        [listeners] => 0
                    )

            )

        [created_at] => 2012-01-15T04:03:34+01:00
        [updated_at] => 2016-04-26T19:45:54+02:00
    )

[1] => Array
    (
        [id] => 7
        [name] => Powerfmse
        [country] => SE
        [image] => Array
            (
                [url] => 
                [thumb] => Array
                    (
                        [url] => 
                    )

            )

        [slug] => powerfmse
        [website] => http://powerfm.se
        [twitter] => 
        [facebook] => 
        [total_listeners] => 0
        [categories] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [title] => Dance
                        [description] => dance music, the new from 80's and 90's, like bubblegum and more.
                        [slug] => dance
                        [ancestry] => 14
                    )

                [1] => Array
                    (
                        [id] => 14
                        [title] => Electronic
                        [description] => all computeriz made.
                        [slug] => electronic
                        [ancestry] => 
                    )

                [2] => Array
                    (
                        [id] => 1
                        [title] => Trance
                        [description] => stations that plays commercial and other things in trance-music genre.
                        [slug] => trance
                        [ancestry] => 14
                    )

            )

        [streams] => Array
            (
                [0] => Array
                    (
                        [stream] => http://media.powerfm.se:80/high
                        [bitrate] => 160
                        [content_type] => ?
                        [status] => 1
                        [listeners] => 0
                    )

                [1] => Array
                    (
                        [stream] => fffff
                        [bitrate] => 0
                        [content_type] => ?
                        [status] => 0
                        [listeners] => 0
                    )

            )

        [created_at] => 2012-01-15T05:53:31+01:00
        [updated_at] => 2015-04-11T14:10:44+02:00
    )

我需要从[stream]的类别和值中获取[description]的值。我只显示[0]数组到[1],有超过20个。

使用for循环来获取所有数据。

<body>
<?php

$json_string = 'http://api.dirble.com/v2/stations?
token=123';

$jsondata = file_get_contents($json_string);

$obj =  json_decode($jsondata,true);

?>

<p id="demo"></p>

<script>

var i, j, x = "";

for (i in obj.categories) {

    x += "<h2>" + obj.categories[i].description + "</h2>";

}

document.getElementById("demo").innerHTML = x;

</script>
</body>
你能帮帮我吗?我也可以尝试在php中显示每个使用,有什么帮助吗?感谢。

1 个答案:

答案 0 :(得分:0)

你应该做这样的事情,

foreach(YOUR_MAIN_ARRAY as $value) {
    foreach($value["categories"] as $tmpValue) {
        foreach($tmpValue as $description){
            echo $description["description"];
        }
    }

    foreach($value["streams"] as $tmpValue) {
        foreach($tmpValue as $stream){
            echo $stream["stream"];
        }
    }
}

它包含语法错误,因为我没有测试过,但逻辑很清楚。