如何从json中获取数据php(多维数组)



这是我使用的数据:

{
    "jornadas": [
        {
            "partits": [
                {
                    "local": 1,
                    "visitante": 2
                },
                {
                    "local": 3,
                    "visitante": 4
                },
                {
                    "local": 5,
                    "visitante": 6
                }
            ]
        },
        {
            "partits": [
                {
                    "local": 1,
                    "visitante": 2
                },
                {
                    "local": 3,
                    "visitante": 4
                },
                {
                    "local": 5,
                    "visitante": 6
                }
            ]
        },
        {
            "partits": [
                {
                    "local": 1,
                    "visitante": 2
                },
                {
                    "local": 3,
                    "visitante": 4
                },
                {
                    "local": 5,
                    "visitante": 6
                }
            ]
        },
        {
            "partits": [
                {
                    "local": 1,
                    "visitante": 2
                },
                {
                    "local": 3,
                    "visitante": 4
                },
                {
                    "local": 5,
                    "visitante": 6
                }
            ]
        },
        {
            "partits": [
                {
                    "local": 1,
                    "visitante": 2
                },
                {
                    "local": 3,
                    "visitante": 4
                },
                {
                    "local": 5,
                    "visitante": 6
                }
            ]
        }
    ]
}

当它进入php时,我解码它print_f结果是:

([jornada] => stdClass对象数组([0]=> stdClass对象数组([partits] => ([0] => stdClass对象((本地)=> 2 [visitante] => 7) [1] => stdClass对象((本地)=> 3 [visitante] => 5) [2] => stdClass对象((本地)=> 4 [visitante] => 1))) [1] => stdClass对象数组([partits] => ([0] => stdClass对象((本地)=> 1 [visitante] => 7) [1] => stdClass对象((本地)=> 5 [visitante] => 4) [2] => stdClass对象((本地)=> 2 [visitante] => 3))) [2] => stdClass数组对象([partits] => ([0] => stdClass对象((本地)=> 3 [visitante] => 7) [1] => stdClass对象((本地)=> 4 [visitante] => 2) [2] => stdClass对象((本地)=> 1 [visitante] => 5))) [3] => stdClass对象数组([partits] => ([0] => stdClass对象((本地)=> 5 [visitante] => 7) [1] => stdClass对象((本地)=> 2 [visitante] => 1) [2] => stdClass对象((本地)=> 3 [visitante] => 4))) [4] => ([partits] => stdClass对象数组([0]=> stdClass对象([local] => 4 [visitor] => 7) [1] => stdClass Object ([local] => 1 [visitor] => 3) [2] => stdClass Object ([local] => 5 [visitor] => 2)))))

嗯,我不知所措,因为我无法从这个json中检索任何数据。如果您能给我任何建议,我将不胜感激。

这个答案假设您正在使用PHP的json_decode函数。

您应该将第二个参数解码为true:

json_decode($yourJSON, true);

这给了你一个关联数组,而不是stdClass对象。

json_decode功能的更多信息:http://php.net/manual/en/function.json-decode.php

try foreach after decode

$decode = json_decode($json, true); //decodes in associative array
foreach($decode['jornadas'] as $k=>$v) {
  foreach($v['partits'] as $k1=>$v1) {
     echo $v1['local'];
     echo $v1['visitante'];
  }
}

相关内容

  • 没有找到相关文章