简体   繁体   English

PHP If语句->

[英]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 当进入if部分时,我抛出一个错误

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. 如果要使用对象数组,请不要将其强制转换为数组(或按照建议在json_decode中强制使用数组。

The most important thing is to understand the structure of the JSON string and how this will behave when decoded into an object/array. 最重要的是了解JSON字符串的结构,以及在解码为对象/数组时该字符串的行为。

Also it looks like you might not have closed out your curly braces from first if statement. 同样,看起来您可能还没有从第一个if语句开始就关闭了花括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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