douyue7408 2010-09-02 14:01
浏览 37

PHP无法访问数组中的对象

in my application i get returned an array of objects and i am trying to access to access the object inside the array like this $array[0]->name but it doesn't work for some reason ,

i tried to access it like this - $var = $array[0]; $var->name and it shows me this error > Trying to get property of non-object

whats going on here ? why i cant access the object ? and how can i access it ? also , would be better if i just got a multidimensional array instead array of objects?

var_dump://

array(2) {
  [0]=>
  object(stdClass)#16 (9) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(27) "нямам си и идея"
    ["description"]=>
    string(19) "емиииии....."
    ["price"]=>
    string(3) "823"
    ["lang"]=>
    string(2) "bg"
    ["category"]=>
    string(1) "0"
    ["slug"]=>
    string(7) "shalala"
    ["status"]=>
    string(1) "1"
    ["sid"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#17 (9) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(19) "no tyore idea what "
    ["description"]=>
    string(22) "are you talking to me "
    ["price"]=>
    string(3) "823"
    ["lang"]=>
    string(2) "en"
    ["category"]=>
    string(1) "0"
    ["slug"]=>
    string(7) "shalala"
    ["status"]=>
    string(1) "1"
    ["sid"]=>
    string(1) "1"
  }
}

vardump of $array[0]

object(stdClass)#16 (9) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(27) "нямам си и идея"
  ["description"]=>
  string(19) "емиииии....."
  ["price"]=>
  string(3) "823"
  ["lang"]=>
  string(2) "bg"
  ["category"]=>
  string(1) "0"
  ["slug"]=>
  string(7) "shalala"
  ["status"]=>
  string(1) "1"
  ["sid"]=>
  string(1) "1"
}

[resolved] = the framework was pointing me at the wrong line of code the error was 10 lines down the code and i didnt see it :(

  • 写回答

1条回答 默认 最新

  • duangu3620 2010-09-02 14:04
    关注

    The issue is somewhere else.

    The $array[0] is NOT an object that's why it's saying Trying to get property of non-object.

    评论

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面