Hướng dẫn php array contains value
|
(PHP 4, PHP 5, PHP 7, PHP 8)
Show
in_array — Checks if a value exists in an array Descriptionin_array(mixed Parametersneedle
The searched value.
haystack
The array. strict
If the third parameter
Return ValuesReturns ExamplesExample #1 in_array() example
The second condition fails because in_array() is case-sensitive, so the program above will display: Example #2 in_array() with strict example
in_array('12.4', $a, true)) {
in_array(1.13, $a, true)) {
The above example will output: 1.13 found with strict check Example #3 in_array() with an array as needle
in_array(array('p', 'h'), $a)) {
in_array(array('f', 'i'), $a)) {
in_array('o', $a)) {
The above example will output: 'ph' was found 'o' was found See Also
beingmrkenny at gmail dot com ¶ 10 years ago
in_array(null, $array); // true
in_array(null, $array, true); // true
rhill at xenu-directory dot net ¶ 13 years ago
= array(
echo in_array($needle, $haystack, true) ? 'true' : 'false';// Output is 'false'echo in_array($needle, $haystack) ? 'true' : 'false'; // Output is 'true'?> I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode. thomas dot sahlin at gmail dot com ¶ 12 years ago
= array('apple', 'banana', 'orange');
if ( in_array('banana', $slow))print('Found it!');$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange'); if (isset( $fast['banana']))print('Found it!');?> |
