0

How do I read this array?

stdClass Object ( 
  [0] => stdClass Object ( 
    [role_id] => 1 
    [username] => [email protected] 
    [date_time] => 2013-01-13 14:01:00
  ) 
  [status] => success 
) 

I can read the status as $objectname->status but I am not able to rwad $objectname->username.

5
  • 4
    try objectname[0].username Commented Jan 30, 2013 at 7:01
  • 2
    This is PHP, not JavaScript. Suggest to use $objectname->{"0"}->username. Commented Jan 30, 2013 at 7:12
  • WORKED. Thank you friend! Commented Jan 30, 2013 at 7:15
  • Wow, numeric properties .. didn't think I'd ever get to see it. Commented Jan 30, 2013 at 7:16
  • Jack - is there something wrong using it that way? Just curious Commented Jan 30, 2013 at 7:19

1 Answer 1

1

That's because username is a property of $objectname->{"0"}, which is also an object.

Try this:

$json='{"0":{"role_id":1,"username":"[email protected]","date_time":"2013-01-13 14:01:00"},"status":"success"}';
$obj=json_decode($json);
var_dump($obj);
var_dump($obj->{"0"}->username);

Live example

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

Comments

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.