1

I have a simple answer I just can't solution to:

var_dump(obj) =

object(stdClass)#15 (3) {
  ["properties"]=>
  object(stdClass)#14 (2) {
    ["user_name"]=>
    string(4) "somename"
    ["email_address"]=>
    string(12) "[email protected]"
  ["arrays"]=>
  object(stdClass)#17 (1) {
    ["sites[]"]=>
    object(stdClass)#18 (4) {
      ["0"]=>
      int(1)
      ["1"]=>
      int(1)
      ["2"]=>
      int(0)
      ["3"]=>
      int(0)
    }
  }
}

How to call the 'sites[]' object in my 'obj'?

I tried the following:

obj->sites[]
obj->{'sites[]'}

Both options aren't working...

1
  • @Machavity, the variable being dumped is not an array, it's an stdClass object. Commented Nov 13, 2015 at 16:18

1 Answer 1

3

It might be better to clean up the code that generates that object, but you should be able to access the sites[] object via:

$sites = $obj->arrays->{'sites[]'};

However $sites will still be an object, so you would need to access its elements in a similarly awkward way:

echo $sites->{'0'};

It would be better to cast it to an array at that point:

$sites = (array) $obj->arrays->{'sites[]'};

Then you can access as an array:

echo $sites[0];

EDIT, seams you cannot access array elements indexed by a numerical string.

A better option (as discovered by a SO question i just posted about this) would be to use get_object_vars:

$sites = get_object_vars($obj->arrays->{'sites[]'});

Then you can access as an array:

echo $sites[0];
Sign up to request clarification or add additional context in comments.

1 Comment

Lol, I forgot to add 'arrays' there, dumb me. I already cleaned up the naming. Thank you for your help

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.