3

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!

2
  • Do you want to get only the name and rate on the first index? Commented Aug 13, 2015 at 1:08
  • Of the first and of the second index. Commented Aug 13, 2015 at 1:10

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

No problem sir @DraugDev. Glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.