douyan8772 2012-11-11 21:23
浏览 16
已采纳

来自JSON数据的PHP IF函数

I have the following data, when doing var_dump() on my data:

  ["time_zone"]=>
   string(6) "London"
  ["geo_enabled"]=>
   bool(false)
  ["verified"]=>
   bool(true)

I can do the following to access the string "London": $UserTimeZone = $userProfile->time_zone;

However I also need to be able to determine whether the "verified" bool value is true/false.

How can I write an if to check if the "verified" bool value is true/false.

I've tried the following and it isn't working it always returns - not verified:

$verified->verified;

if ($verified) {
  echo "verified";
} else {
  echo "not verified";
}

Any help appreciated.

  • 写回答

1条回答 默认 最新

  • dongping4901 2012-11-11 21:27
    关注

    It doesn't look like you are actually setting the variable $verified, in which case it would always result as false in your if statement.

    $verfied->$verified; // This isn't setting the variable $verified to anything.
    
    $verified = $userProfile->verified; // This sets the variable $verified to what is stored in your user profile object
    
    if ($verified) {
      echo "verified";
    } else {
      echo "not verified";
    }
    

    Or, you could do:

    if ($userProfile->verified) {
      echo "verified";
    } else {
      echo "not verified";
    }
    

    This way you don't have to worry about setting any extra variables, you're just accessing what is stored in the object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大