This works:
if( in_array('jb-svg', $_POST) || in_array('both', $_POST) || in_array('highcharts', $_POST) ):
echo 'NO PROBLEM';
endif;
This doesn’t
if( in_array( array('jb-svg', 'both', 'highcharts'), $_POST) ):
echo 'BIG PROBLEM';
endif;
This works:
if( in_array('jb-svg', $_POST) || in_array('both', $_POST) || in_array('highcharts', $_POST) ):
echo 'NO PROBLEM';
endif;
This doesn’t
if( in_array( array('jb-svg', 'both', 'highcharts'), $_POST) ):
echo 'BIG PROBLEM';
endif;
It’s a bit strange but when an array is passed as the first argument, the second one must contain the same array. Not array elements. From the manual http://php.net/manual/en/function.in-array.php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
I suspect array_reduce could be used to implement the desired functionality.
Many thanks for the prompt reply. I checked the manual and incorrectly thought using an array would work OK and was most surprised when it failed on all three occurrences!
array_reduce(…) seems far too complicated and overkill.
I have three form input type submit buttons and the first option is verbose but at least it works OK. I will stick to using the verbose version.
You could do an array_intersect and see if the result has at least one entry.
Or you could use preg_grep
, like
if (preg_grep('/jb-svg|both|highcharts/', $_POST) {
echo "No problem!";
}
An array of arrays ((red, blue), (green, black)) is not the same as an array of elements (red, blue, green, black).
if(array_intersect(['jb-svg', 'both', 'highcharts'], $_POST)):
Meanwhile I decided to test for $postSubmit and use strpos(…) because $postSubmit is used in other sections of the script and I prefer testing for a numeric instead of an empty array().
$postSubmit = isset($_POST['submit']) ? $_POST['submit'] : NULL;
if( $postSubmit && strpos('\jb-svgbothhighcharts', $postSubmit) ):
echo 'SUCCESS';
endif;
Many thanks for the examples which I will certainly consider in the future.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.