简体   繁体   中英

PHP cannot access object in array

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 $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 :(

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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