0

I have a multidim array that I can view using print_r($users):

Array
(
 [0] => stdClass Object
 (
 [username] => crustacea
 [created_on] => 2010年08月07日 19:54:32
 [active] => 1
 )
)

I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.

I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?

asked Aug 8, 2010 at 21:48

2 Answers 2

1

$user[0] is not an array but an object as indicated by [0] => stdClass Object.

echo $users[0]->username;
answered Aug 8, 2010 at 21:50
Sign up to request clarification or add additional context in comments.

Comments

1

$user[0] is object and not an array. You can access your value by doing

$users[0]->username;

Or you could cast/convert your object into an array like

((array)$users[0])['username'];
answered Aug 8, 2010 at 21:57

1 Comment

I am accepting the other answer merely because it came in earlier. I appreciate the extra tip on casting as array. Thanks very much.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.