简体   繁体   中英

PHP If statement ->

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!

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";}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM