2

I currently have an Object which holds a series of Arrays, when I do print_r it does show the arrays however, when I want to get these values out I seem to be getting an error.

print_r($Obj->Example);

Returns:

Object => Example ( 'Username' => 'Example' )

My code to query through the Object is:

foreach($Obj as $single):
    echo $Example['Username'];
endif;

Is it possible to query through like this because it isn't working and I get an error saying that:

$Obj is not defined as an Array

So how can I access the Example array and echo all the Usernames ?

11
  • 1
    try this $Exampale->username and use var_dump() function for dumping variable. don't use print_r Commented Mar 11, 2016 at 15:52
  • 1
    Please show us your real output from print_r() Commented Mar 11, 2016 at 15:52
  • 1
    @Naumov Besides that variables are case-sensitive, why shouldn't OP use print_r()? Commented Mar 11, 2016 at 15:52
  • 1
    you can foreach objects, but they have to implement iterable. without that, you can NOT use foreach on an object, because it's NOT an array. Commented Mar 11, 2016 at 15:53
  • print_r() returns me what I am looking for and it is an array but when I foreach the Object to get to the Array it gives me an error. How do I foreach the Object? Commented Mar 11, 2016 at 15:53

1 Answer 1

3

As Example is an array that belongs to an object, the code would be like this

foreach($Obj->Example as $single):
    echo $single['Username'];
endif;
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, after reading the comments I kinda widened the way I was looking at this concept. I can't iterate through the Object to get to the Array, I'd have to declare that I wanted to iterate through the Array inside my Class the Object. Thanks for this! I'll mark it when I can! Finally, I can instance and use the Arrays inside it! xD

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.