【问题标题】:PHP : Notice: Undefined property: stdClass::PHP:注意:未定义的属性:stdClass::
【发布时间】:2016-11-12 09:52:26
【问题描述】:

我正在从名为properties 的表中获取json 数据。列名为 attr,其中包含 size、bedrooms 和 prop-type。

  $q= mysqli_query($connect,"SELECT * FROM properties");
    $savemyval = array();
    while($row= mysqli_fetch_assoc($q)){
        $data = json_decode($row['attr']);
          //var_dump($data);
          if($proptpe == $data->proptype){
                $savemyval[] = $row['id'];

            }
  }

如果我 var_dump 这就是我得到的,像上面一样查询数据

object(stdClass)[3]
  public 'bedrooms' => string '5' (length=1)
  public 'proptype' => string 'residential' (length=11)
object(stdClass)[4]
  public 'bedrooms' => string '4' (length=1)
  public 'proptype' => string 'commercial' (length=10)
object(stdClass)[3]
  public 'size' => string '16000' (length=5)
  public 'prop-type' => string 'commercial' (length=10)

在 var_dump 中我得到了正确的数据,但是当我尝试获取 proprtype 时,如果它大于 1,它会给我错误

PHP : 注意:未定义的属性:stdClass::

如果我使用 isset 则没有错误,但它仍然会打印一个结果,而转储给我的结果不止 1 个

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    因为在您的数组中,您的第二个索引上没有 proptype

    这样试试

    if(isset( $data['proptype']) && $proptpe == $data->proptype){
           $savemyval[] = $row['id'];
    
    }
    

    【讨论】:

    • 这是我得到的致命错误:不能使用 stdClass 类型的对象作为数组
    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 2016-02-22
    • 2016-10-03
    • 1970-01-01
    • 2016-04-17
    • 2015-02-27
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多