Jump to content
littleangel

the foreach in the loop returns bool(false) though array is present

Recommended Posts

in my code below the **data is extracted correctly for the first foreach but doesn't return anything for 2nd foreach though selected one present inside the 2nd foreach** i still get the error Warning: Invalid argument supplied for foreach() for line `foreach ($authorlist as $post)`... however the 2nd foreach returns data correctly as soon as i remove the first foreach loop. 

 

below is my code

 

  


  <?php
    
    $all= get_posts(array('post_type' => 'books', 'numberposts' => -1,));
    
    foreach ( $all as $post ) :  setup_postdata($post);
    
        if (!empty($post))
        {
         $booklink = array();
    
         $booklist = get_field('booklist'); 
                   foreach ($booklist as $post) : setup_postdata($post);
                        if (!empty($post))
                            {
                            $booklink[] = strip_tags(get_field('booklink',$post));                                       
                            }
                    endforeach;
                    wp_reset_postdata();
         echo $booklink[0];  
    
         $authorname = array();
    
         $authorlist = get_field('authorlist'); 
                   foreach ($authorlist as $post) : setup_postdata($post);
                        if (!empty($post))
                            {
                            $authorname[] = strip_tags(get_field('authorname',$post));                                       
                            }
                    endforeach;
                    wp_reset_postdata();
    echo $authorname[0]; 
    
        }
    endforeach;
    ?>

 

i did var_dump($authorlist) ...it returns bool(false) as long as foreach ($booklist as $post)  loop is present. The moment i remove that loop then the dump shows the data of the authorlist correctly...how to get both the loops working together.

 

Edited by littleangel

Share this post


Link to post
Share on other sites

Not enough info.  Post the array you are working with.  There are a couple of custom functions in the code as well, we have no idea what those contain.  It could all lead back to your error.

Share this post


Link to post
Share on other sites

Because you foreach as $post in all of them. $post gets overwritten in the inner-loops. Name them different in each loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.