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?
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 beFALSE
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 thanFALSE
would suffice: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
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.