0

Here is my problem

CODE:

$response = json_decode($data);

$arr = (array) $response;

if(is_array($arr['reviews'])){

foreach($arr['reviews'] as $review){

if ($review->rating == "0") {echo "<div class="0stars"></div>\n";}

} 

It throws me an error when it gets to the if portions

Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting ',' or ';' in /home....

I've tried:

$reviewstar = $review->rating;
if ($reviewstar == "0") {echo "<div class="0stars"></div>\n";}

and many other options....

How can I do that? do I need to array again?

Thanks!

6
  • are you missing a closing parenthesis? Commented Sep 19, 2013 at 20:16
  • 1
    What error does it throw? There's no problem with your if() syntax, maybe the problem is that $review doesn't have a rating property? Commented Sep 19, 2013 at 20:17
  • 3
    "Throws me an error" without the error message is meaningless. Commented Sep 19, 2013 at 20:17
  • 7
    Instead of doing the array cast add true as the second parameter to json_decode.... $response = json_decode($data, true); Commented Sep 19, 2013 at 20:17
  • Sorry, for the miss explanation, what I want to do is when in the foreach I want to find out the value for the rating than if it matches the number 0 I want it to echo <div class="0stars"></div> Commented Sep 19, 2013 at 20:27

2 Answers 2

1

The quotes in this line are broken:

if ($review->rating == "0") {echo "<div class="0stars"></div>\n";}

Change it to:

if ($review->rating == "0") {echo "<div class=\"0stars\"></div>\n";}
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to cast to array, but then use object type access here:

$review->rating

If you want to work with an array of objects the don't cast to an array (or force array in json_decode as has been suggested.

The most important thing is to understand the structure of the JSON string and how this will behave when decoded into an object/array.

Also it looks like you might not have closed out your curly braces from first if statement.

1 Comment

Ok here is how it goes json-> array only the "reviews"-> for each review there is a rating -> I want to replace the rating number in each review inside of the foreach with some html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.