Getting this error: "Warning: in_array() expects parameter 2 to be array, null given in..."

I'm using Advanced Custom Fields. I've got a select option and a conditional statement if nothing is selected. This code works fine when there is either an Award or Review or both selected. But when nothing is selected I get the following errors (same one twice):

Warning: in_array() expects parameter 2 to be array, null given in /home/intrepid/public_html/sandbox/bruce-hale/wp-content/themes/bruce-hale/template-parts/content-book.php on line 38

Warning: in_array() expects parameter 2 to be array, null given in /home/intrepid/public_html/sandbox/bruce-hale/wp-content/themes/bruce-hale/template-parts/content-book.php on line 38


Here is the code I'm using:

<?php 
				$selected = get_field('any_reviews_awards');
				if( is_array($selected) || in_array('awards', $selected) || in_array('reviews', $selected )) { ?>
				
			<div class="praise-award">
					
		<?php if( have_rows('award')): ?>
		
		<?php while( have_rows('award') ): the_row(); ?> 
			<p class="award praise"><em><?php the_sub_field('award_source'); ?></em> <span><?php the_sub_field('award'); ?></span></p>
			<?php endwhile; ?>
			<?php endif; ?>
			<?php if(have_rows('review') ): ?>
			<?php while( have_rows('review') ): the_row(); ?>
			<blockquote class="review praise"><?php the_sub_field('review_text'); ?> <cite><?php the_sub_field('review_source'); ?></cite></blockquote>
			<?php endwhile; ?>
			<?php endif; ?>
		</div><!-- end .praise -->
		<?php } ?>

Open in new window

LVL 9
Alicia St RoseOwner & Principle Developer/DesignerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
I managed to fix the error by editing my code in this fashion:

<?php if( get_field('any_reviews_awards') !=false) { ?>
						
			<div class="praise-award">
				
		<?php if(in_array('awards', get_field('any_reviews_awards'))) { ?>
			
		
		<?php if( have_rows('award')): ?>
		
		<?php while( have_rows('award') ): the_row(); ?> 
			<p class="award praise"><em><?php the_sub_field('award_source'); ?></em> <span><?php the_sub_field('award'); ?></span></p>
			<?php endwhile; ?>
			<?php endif; ?>
			<?php } ?>
			<?php if(in_array('reviews', get_field('any_reviews_awards'))) { ?>
			<?php if(have_rows('review') ): ?>
			<?php while( have_rows('review') ): the_row(); ?>
			<blockquote class="review praise"><?php the_sub_field('review_text'); ?> <cite><?php the_sub_field('review_source'); ?></cite></blockquote>
			<?php endwhile; ?>
			<?php endif; ?>
			<?php } ?>
		</div><!-- end .praise -->
		<?php } ?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alicia St RoseOwner & Principle Developer/DesignerAuthor Commented:
I kept picking a way at it and found the solution! Yay me!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.