1

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

7
  • The error message says $array[0] isn't an object. Use var_dump() on it to see what's inside. Commented Sep 2, 2010 at 14:03
  • 3
    Can you paste the context of the code? It looks like there is a typo somewhere. Commented Sep 2, 2010 at 14:10
  • Grodon , thats the output of the $array , in a sec iil add the $array[0] Commented Sep 2, 2010 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. Commented Sep 2, 2010 at 14:15
  • 1
    What happens when you run this code: $a = new StdClass; $a->name = 'foo'; $array = array($a); echo $array[0]->name; Commented Sep 2, 2010 at 14:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

that doesnt solve the issue and therefor should have been a comment
@Gordon, OK will remember next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.