2

I have a PHP key/value array and I want to grab a value from there and display it in a div.

So far I have:

$homepage = file_get_contents('http://graph.facebook.com/1389887261/reviews');
$parsed = json_decode($homepage);

I want to get the values out of the key/value pair array like this:

foreach ($parsed as $key => $values){
    echo $values['rating'];
}

But this does not retrieve the value. What am I doing wrong?

0

8 Answers 8

3

Use the PHP foreach index reference, this gives you the ability to grab the key or the value.

$parsed = json_decode($homepage,true);
foreach ($parsed['data'] as $key => $values){
    echo $values['rating'];
}

http://www.php.net/manual/en/control-structures.foreach.php

Sign up to request clarification or add additional context in comments.

3 Comments

i get Cannot use object of type stdClass as array
This is not correct. $parsed is a stdClass object, not an array.
check freddy's answer, you'll need to provide true as the second argument in $parsed = json_decode($homepage,true);. Updated my answer with it as well.
2
$rating = $parsed->data[0]->rating;

Does it work for you?

5 Comments

i get Cannot use object of type stdClass as array
@Patrioticcow I have modified my answer - the first version would work, if json_decode() would return associative array (which is not the case here).
@downvoter The reason you downvoted me is because it returned this error or something else?
removed the downvote when you corrected the factual error in the code sample (associative array vs stdClass) :)
@Chris Ok, thanks :) I apologize - I did not tested it before posting and did not find anything was wrong. But indeed there is no reason to get it as associative array.
1
foreach ($parsed['data'] as $key => $values){
  echo $values['rating'];
}

Note, json_decode() returns object by default. You need to update the following to do the above:

$parsed = json_decode($homepage, true);

Comments

1

done by dumping your example)

foreach ($parsed->data as $key => $values){
echo $values->rating;}

Comments

0

If you don't pass the second parameter, you'll get back an object instead of an array, see http://php.net/json_decode

$parsed = json_decode($homepage,true);
foreach ($parsed['data'] as $key => $values){
    echo $values['rating'];
}

will do the trick for you.

Comments

0

The values are stdClass objects, not arrays. So, to loop:

$homepage = file_get_contents('http://graph.facebook.com/2345053339/reviews');
$parsed = json_decode($homepage);
foreach($parsed->data as $values) {
    echo 'Rating:'.$values->rating;
}

Note I am using the -> operator to access object properties...

Comments

-1

Rating is a subarray of array ['data'], so:

foreach ($parsed['data'] as $key => $values){
    echo $values['rating'];
}

Comments

-1

The root node for "parsed" is data which is an array so you probally want..

foreach($parsed['data'] as $key => $value) {
  echo $value['rating'];
}

3 Comments

This is not correct. $parsed is a stdClass object, not an array.
@Chris, ease up on the downvotes. Most of these answers have updates.
The answers are totally wrong, non-functional code. It's all well and good to flood a bunch of answers onto a question, if every single one of them is a duplicate of the other wrong answers, what is the utility in that? Hence downvote. This isn't a race or a contest - the idea is to factually answer a question. Eh?

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.