使用PHP进行JSON解析获取信息

时间:2015-08-13 01:03:25

标签: php arrays json foreach

我试图用PHP从JSON数据结构中解析一些信息。我的foreach工作真的很奇怪:

[query] => Array (
 [count] => 2
 [created] => 2014-05-12
 [lang] => de-DE
 [results] => Array (
     [rate] => Array (
         [0] => Array (
             [id] => 1
             [Name] => User1
             [Rate] => 64.5245
             [Date] => 8/13/2013
             )
         [1] => Array (
             [id] => 2
             [Name] => User2
             [Rate] => 71.9697
             [Date] => 8/3/2014
             )
         )
     )
 )

我需要解析名称评分已创建(从数组开头的日期)。

我的代码是:

foreach ($json_var['query']['results']['rate'][0] as $key=>$value) {
    echo $value['Name'];
}

但是我遇到了很多错误。如果我在没有[0]的情况下尝试,我会得到两个用户的名字。

你能帮我吗?非常感谢你!

1 个答案:

答案 0 :(得分:5)

试试这个:希望这有助于你

$newArray = [];
   foreach ($json_var['query']['results']['rate'] as $key=>$value) {
     $newArray['NameAndRate'][] = ['Name' =>$value['Name'], 'Rate' =>$value['Rate']];
     $newArray['create'] = $jsonVar['query']['create'];
    }
print_r($newArray);