3

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?

1
  • 3
    Have you tried is_array(get_field('episode_sdversion')) or if (get_field('episode_sdversion') !== false) ? Something like if (get_field('episode_sdversion') !== false && in_array('Subbed', get_field('episode_sdversion'))) should do. Commented Feb 24, 2013 at 22:25

3 Answers 3

2

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 '--';
} ?>
Sign up to request clarification or add additional context in comments.

Comments

2

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')))

Comments

1

You either have to change that code, or the get_field return value. A way to do it, would be to declare an array() in all cases, and then add every posted checkbox, so that you always have an array passed as the second parameter of the in_array function.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.