PHP有效地访问不同级别的JSON数组中的值

时间:2017-10-18 21:37:28

标签: php arrays json foreach

处理一个个人项目,该项目将从API中提取每个宠物小精灵的完整详细信息。

到目前为止,我获得了URL的内容并将结果返回到JSON数组格式。

目前我一直试图以有效的方式从阵列中检索[stats]的结果。

private function getGenOnePokemon()
{
    // the url of the api 
    $url = $this->baseUrl;

    //get the contents of $url var and decode it into a json array
    $json = file_get_contents($url , true);
    $pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);

    // array output of pokemon
    echo '<pre> ';
    print_r($pokemon);
    echo'</pre>';


    //echo out value  as speed

    foreach($pokemon['results'][0] as $happy)
    {
        echo $happy['name'] . '<br />';
    }


    // echo base_stat value for speed with value of 90
    echo $pokemon['stats'][0]['base_stat'];


}

但是我似乎没有得到任何打印值/键,因为我需要添加其他内容才能完全访问这些值?

宁愿不直接访问结果,就像我正在使用base_stat作为计划使用此逻辑稍后传递到HTML视图层。

print_r转储的示例(非完全转储为真的很长)完整示例:https://pokeapi.co/api/v2/pokemon/pikachu

Array
(
[forms] => Array
    (
        [0] => Array
            (
                [url] => https://pokeapi.co/api/v2/pokemon-form/25/
                [name] => pikachu
            )

    )

[abilities] => Array
    (
        [0] => Array
            (
                [slot] => 3
                [is_hidden] => 1
                [ability] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/ability/31/
                        [name] => lightning-rod
                    )

            )

        [1] => Array
            (
                [slot] => 1
                [is_hidden] => 
                [ability] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/ability/9/
                        [name] => static
                    )

            )

    )

[stats] => Array
    (
        [0] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/6/
                        [name] => speed
                    )

                [effort] => 2
                [base_stat] => 90
            )

        [1] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/5/
                        [name] => special-defense
                    )

                [effort] => 0
                [base_stat] => 50
            )

        [2] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/4/
                        [name] => special-attack
                    )

                [effort] => 0
                [base_stat] => 50
            )

        [3] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/3/
                        [name] => defense
                    )

                [effort] => 0
                [base_stat] => 40
            )

        [4] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/2/
                        [name] => attack
                    )

                [effort] => 0
                [base_stat] => 55
            )

        [5] => Array
            (
                [stat] => Array
                    (
                        [url] => https://pokeapi.co/api/v2/stat/1/
                        [name] => hp
                    )

                [effort] => 0
                [base_stat] => 35
            )

    )

有关如何使用foreach或其他提示访问数据的任何建议都非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

您的$pokemon数组不包含results字段。只有forms字段。因此,您应该遍历forms以打印表单的名称。

foreach($pokemon['forms'] as $happy) {
    echo $happy['name'] . '<br />';
}

你可以用统计数据做同样的事情

foreach($pokemon['stats'] as $stat) {
    $base_stat = $stat['base_stat'];
    // ...
}