0

This should be simple but I can't figure it out...I have this array in php:

Array ( [108] => WP_Post Object ( [ID] => 108 [post_author] => 1 [post_date] => 2020-07-17 19:25:21 [post_date_gmt] => 2020-07-18 00:25:21 [post_content] => [post_title] => Sight_to_See_paperback_final13-10-10-19 [post_excerpt] => [post_status] => inherit [comment_status] => open [ping_status] => closed [post_password] => [post_name] => sight_to_see_paperback_final13-10-10-19-2 [to_ping] => [pinged] => [post_modified] => 2020-11-29 16:09:42 [post_modified_gmt] => 2020-11-29 21:09:42 [post_content_filtered] => [post_parent] => 105 [guid] => https://legacytoo.com/wp-content/uploads/2020/07/Sight_to_See_paperback_final13-10-10-19-1.pdf [menu_order] => 0 [post_type] => attachment [post_mime_type] => application/pdf [comment_count] => 0 [filter] => raw )

How do I get the value of "108" in my code? Or, how do I get the guid value when I don't have the 108 value. I've get the array from this:

   $query = array(
        'post_parent'       => get_the_ID(),
        'post_type'         => 'attachment',
        'post_mime_type'    => 'application/pdf');

    $images = get_children( $query );

and I try to display it like this:

$thisVar = $images->guid; 

and this

$thisVar = $images[0]->guid;

and neither works. This one works but again, I don't have the 108 value:

$thisVar = $images[108]->guid;

Thanks if you can help.

4
  • 1
    php.net/manual/en/control-structures.foreach.php Commented Dec 7, 2020 at 23:40
  • 1
    What output do you get if you do reset($images), reset() will give you the first element in the array. Commented Dec 7, 2020 at 23:41
  • You are a genius, that works, thanks. Commented Dec 7, 2020 at 23:44
  • 1
    Putting it as an answer for future reference :) Commented Dec 7, 2020 at 23:49

1 Answer 1

2

In this situation you can use reset($images) to get the first element in the array. reset() will move the pointer to the first element returning the value of the element or false if empty.

More info: https://www.php.net/manual/en/function.reset.php

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.