note 53745 modified in function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:45:33 +0000 |
| Subject: | note 53745 modified in function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156519@lists.php.net to get a copy of this message | ||
If you have a multidimensional array filled only with Boolean values like me, you need to use
'strict', otherwise in_array() will return an unexpected result.
Example:
<?php
$error_arr = array('error_one' => FALSE, 'error_two' => FALSE,
array('error_three' => FALSE, 'error_four' => FALSE));
if (in_array (TRUE, $error_arr)) {
echo 'An error occurred';
}
else {
echo 'No error occurred';
}
?>
This will return 'An error occurred' although theres no TRUE value inside the array in any
dimension. With 'strict' the function will return the correct result 'No error
occurred'.
Hope this helps somebody, cause it took me some time to figure this out.
--was--
If you have a multidimensional array filled only with Boolean values like me, you need to use
'strict', otherwise in_array() will return an unexpected result.
Example:
<?
$error_arr = array('error_one' => FALSE, 'error_two' => FALSE,
array('error_three' => FALSE, 'error_four' => FALSE));
if (in_array (TRUE, $error_arr)) {
echo 'An error occurred';
}
else {
echo 'No error occurred';
}
?>
This will return 'An error occurred' although theres no TRUE value inside the array in any
dimension. With 'strict' the function will return the correct result 'No error
occurred'.
Hope this helps somebody, cause it took me some time to figure this out.
http://php.net/manual/en/function.in-array.php