PHP If statement ->


PHP If statement ->

这是我的问题

法典:

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

当它到达if部分时,它会给我一个错误

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

我试过:

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

以及许多其他选择。

我该怎么做?我需要再次阵列吗?

谢谢!

此行中的引号已断开:

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

将其更改为:

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

您正在尝试强制转换为数组,但随后在此处使用对象类型访问:

$review->rating

如果要使用对象数组,请不要强制转换为数组(或按照建议强制数组json_decode。

最重要的是了解 JSON 字符串的结构以及解码为对象/数组时的行为方式。

此外,看起来您可能没有从第一个if语句中关闭大括号。