1

My var_dump($gallery) looks like this:

array(1) 
        { [0]=> object(stdClass)#102 (9) { 
            ["term_id"]=> string(2) "17" 
            ["name"]=> string(5) "Image" 
            ["slug"]=> string(5) "image" 
            ["term_group"]=> string(1) "0" 
            ["term_taxonomy_id"]=> string(2) "19" 
            ["taxonomy"]=> string(18) "gallery" 
            ["description"]=> string(0) "" 
            ["parent"]=> string 
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et               tempus tellus. Integer euismod, est et ultricies tristique, urna ipsum              semper elit, pharetra cursus ligula turpis sed libero. Vestibulum ante              ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;             Suspendisse pellentesque orci sed tellus hendrerit a auctor augue               commodo. Ut nibh lacus, …
            Read more... 
            (1) "0" 
            ["count"]=> string(1) "1" 
            } 
        }

And I'm having trouble getting out data from the inside (in this case I want to echo "image"). For example:

$gallery[] outputs

Fatal error: Cannot use [] for reading in [source file url]

$gallery[0] shows

Catchable fatal error: Object of class stdClass could not be converted to string in [source file url]

$gallery[1], $gallery[2] and so forth are empty.

As far as I know PHP $gallery[0][3] should do the work but how, if I'm unable to echo stdClass object? :/ Is $gallery[0]['slug'] also valid btw?

Thanks a lot.

And yes - I'm unable to change the first item in the array, it's being generated by Wordpress, but I'm asking here because it's strict PHP question.

Cheers.

2 Answers 2

5

$gallery is an array containing one object of type StdClass.

You want to access the slug member of the object held at index 0:

$gallery[0]->slug;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you my master! What's the difference between $gallery[0][slug] and #gallery[0]->slug?
[] is used for array indices, -> is used for access method/member of an object. $gallery[0] is an object, so use ->.
slug is an object property and you need to use it as property if $gallery[0][slug] this wont work. $gallery[0]['slug'] will be as
-1

Full traversing like :

foreach ($gallery as $key=>$value)
{
  print $key;
  print $value;
}

Hope that helps :) And inside, you can get the first $key that would be the object and do it like $key->image

1 Comment

Had to -1, this is just wrong. It's going to cause the same error he's getting when he tries to print $gallery[0]. The issue is he's converting an object to a string, which is exactly what you're trying to do.

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.