Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON parse with PHP get information

I'm trying to parse a few pieces of information from a JSON data structure with PHP. My foreach is working really odd:

[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
             )
         )
     )
 )

I need to parse Name, Rate and created (date from the beginning of the array).

My code is:

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

But I'm getting to many errors. If I try without [0] I get the name of both users.

Can you guys please help me? Thank you very much!

like image 933
DraugDev Avatar asked Feb 09 '26 19:02

DraugDev


1 Answers

Try this: Hope this helps you

$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);
like image 58
aldrin27 Avatar answered Feb 12 '26 09:02

aldrin27