How to see if array is empty before executing foreach?
-
Q: How to check if the array is empty, and break foreach if it is?
All help welcome ^^
-
So, now i’m only looking for an extra eye on the php, is it valid? It works, np, but all feedback is welcome!
For an example of what is possible (and what I did), see the code, and in relevans with my question – is my ‘if’ in the foreach followed by ‘break;’ a good way to go?
<?php $galleryarray = get_post_gallery_ids($post->ID); foreach ($galleryarray as $id) { if ( $id == true ) { $imagesize = wp_get_attachment_image_src( $id, 'medium' ); $attachment_meta = wp_get_attachment( $id ); $media_caption = $attachment_meta['caption']; $media_description = $attachment_meta['description']; ?> <figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);"> <?php if ( $media_caption == true || $media_description == true ) { ?> <input id="info<?php echo $id; ?>" class="nav-check" type="checkbox"> <label for="info<?php echo $id; ?>" class="nav-button info" data-title="Show Info"></label> <figcaption> <?php if ( $media_caption == true ) { ?> <h3><?php echo $media_caption; ?></h3> <?php } if ( $media_description == true ) { ?> <p><?php echo $media_description; ?></p> <?php } ?> </figcaption> <?php } ?> </figure> <?php break; } } ?>hmm… the break; seemes to make the foreach only display one image with a limited number (get_post_gallery_ids($post->ID,4);), likeso:
<?php $galleryarray = get_post_gallery_ids($post->ID,4); foreach ($galleryarray as $id) { if ( $id == true ) { $imagesize = wp_get_attachment_image_src( $id, 'xsmall' ); $attachment_meta = wp_get_attachment( $id ); ?> <figure id="<?php echo $id; ?>" data-name="<?php echo $id; ?>" style="background-image: url(<?php echo $imagesize[0]; ?>);"> </figure> <?php break; } } ?>So I removed the break; from this loop…
But without a limitation number: get_post_gallery_ids($post->ID);
the break; works!Is it important to have the break; or can I just exclude it from both foreach loops?
Any takers?
No, if I use the break; on any of the two loops I get only one image.
so question now:
Q: Is the if ( $id == true ) {} in the foreach a valid approach? And can I have the same variables within two different foreach-loops? (no, I do not want to pass the variable value, just let them have the same name for simplicity.
I gave my two weeks notice last Friday, so in a very short time, I will have a lot of time to dive into a few of these tricky questions.
No biggey,
The Q: can I have the same variables within two different foreach-loops?
A: YesQ: Is the if ( $id == true ) {} in the foreach a valid approach?
A: this says, if variable $id is true, do something – so I think this is valid 🙂
But a better way may be if( !empty($id) ) that says, if the variable is not (the !) empty, do something.Let me konw ^^
Peace & Love
I’ve made changes to fix these issues, and prevent errors if grabbing empty gallery.
The topic ‘How to see if array is empty before executing foreach?’ is closed to new replies.