Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php Code Array boolean returned

So I'm using a checkbox field, and I check it's value by using the code below and print things out accordingly. Anyway, if the field's checkboxes don't have any value, meaning all of them are unchecked, I get an error.

Warning: in_array() expects parameter 2 to be array, boolean given in /filepath.php on line 647

    <?php if (in_array( 'Subbed', get_field('episode_sdversion'))) { ?>
            <a href="<?php echo $episode_permalink; ?>#subbed">Subbed</a>
    <?php } else { 
            echo '--';
    } ?>

So basically, what can I do with this code to make it so when all values are unchecked, that would automatically mean that "Subbed" value is also unchecked, so it should simply just show echo '--';. So how can I make this echo '--'; run when all values are unchecked. So it shouldn't come up with that error?

like image 295
Maaz Avatar asked Dec 04 '25 18:12

Maaz


2 Answers

I'm not sure what your get_field() function does, presumably it's part of a framework or something, but I'm guessing it returns the value of $_REQUEST['episode_sdversion'], which will be FALSE when the checkbox is empty.

In this case, if I understand your question correctly, a simple check to first see if get_field() is returning something other than FALSE would suffice:

<?php if (get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion'))) { ?>
        <a href="<?php echo $episode_permalink; ?>#subbed">Subbed</a>
<?php } else { 
        echo '--';
} ?>
like image 75
Mark Locker Avatar answered Dec 06 '25 07:12

Mark Locker


You are getting the error because get_field returns false instead of an array when no boxes are checked. The && operator is short circuit, meaning that if the first part gets evaluated as false, the second part won't get executed. So you can avoid the error by replacing your first line (if in_array(...)) with

if(get_field('episode_sdversion) && in_array('Subbed', get_field('episode_sdversion')))
like image 44
PhoenixWing156 Avatar answered Dec 06 '25 07:12

PhoenixWing156



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!