up vote 0 down vote favorite

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

link|flag

The error message says $array[0] isn't an object. Use var_dump() on it to see what's inside. – jmz Sep 2 at 14:03
3  
Can you paste the context of the code? It looks like there is a typo somewhere. – Yorirou Sep 2 at 14:10
Grodon , thats the output of the $array , in a sec iil add the $array[0] – Aviatrix Sep 2 at 14:11
3  
Weird. $array[0]->name should work in this case. Use a debugger like Xdebug or Zend_Debugger to step throug the code to see where it goes wrong. – Gordon Sep 2 at 14:15
1  
What happens when you run this code: $a = new StdClass; $a->name = 'foo'; $array = array($a); echo $array[0]->name; – Gordon Sep 2 at 14:44
show 4 more comments

1 Answer

up vote 1 down vote

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.

link|flag
that doesnt solve the issue and therefor should have been a comment – Gordon Sep 2 at 14:06
1  
@Gordon, OK will remember next time. – shamittomar Sep 2 at 14:16

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.