0

So first I am trying to get all pictures from content to take first picture from content and set it as features image if such is not set. I found part of thi online, it seems that this makes loop somehow (i do not get it how actually)

array(1) {  
    [0]=>
    array(4) {
    [0]=>    string(273) "whole image tag with alt"
    [1]=>    string(234) "whole image tag with alt"
    [2]=>    string(202) "whole image tag with alt"
    [3]=>    string(236) "whole image tag with alt"
  }
}

So ... when i uncomment print_r($result); i do get an array that looks like this above. How to access first image tag? Maybe even access src or alt part of that tag? I tried to

echo $result[0];

but i got nothing. Why count is presenting 1 as answer and not 4?

        global $wp_query;
        $post = $wp_query->post;
        $page_id = $post->ID; // page ID
        $page_object = get_page( $page_id ); // page stuff
        //$author_id = $post->post_author; // author ID
        //$author_name = get_the_author_meta( 'display_name', $author_id);
        $page_content = $page_object->post_content;


        //first check all img tags in text              
        preg_match_all('/<img[^>]+>/i',$page_content, $result); 

        print_r($result);


        echo count($result);

1 Answer 1

0

Your array is actually two dimensional. It's in two levels: an array containing an array of four strings.

Try echo $result[0][0];

4
  • yes true. That surely solved problem. To not opening new question, how to count how many array elements are in each dimension?
    – BSheep
    Commented Aug 6, 2016 at 16:19
  • You can use echo count($result, COUNT_RECURSIVE); to get a total. An array of values probably wouldn't be so useful. count( $result[0] ) will tell you how many results you actually have in your case. Commented Aug 6, 2016 at 16:24
  • Thank you. this count( $result[0] ) i could not find anywhere and i knew there was some trick.
    – BSheep
    Commented Aug 6, 2016 at 16:31
  • No problem. Glad to help. Commented Aug 6, 2016 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.