in_array
Description
Syntax
in_array(mixed $needle, array $haystack, bool $strict = false): bool
Parameters
needle
The searched value.
Note: If needle is a string, the comparison is done in a case-sensitive manner.
haystack
The array.
strict
If the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack.
Return
Returns true if needle is found in the array, false otherwise.
Examples
1 · needle haystack
<? $needle = 2; $haystack = array("1", "2", "3"); $return = in_array($needle, $haystack); var_export($return); ?>
true
2 · strict
<? $needle = 2; $haystack = array("1", "2", "3"); $strict = true; $return = in_array($needle, $haystack, $strict); var_export($return); ?>
false
3
<? $haystack = array("Irix", "Linux", "Mac", "NT"); $needle = "Linux"; if(in_array($needle, $haystack)) { echo "$needle\n"; } $needle = "mac"; if(in_array($needle, $haystack)) { echo "$needle\n"; } ?>
Linux
4
<? $haystack = array("1.2", 3.4, 5.6); $strict = true; $needle = "3.4"; if(in_array($needle, $haystack, $strict)) { echo "$needle\n"; } $needle = 5.6; if(in_array($needle, $haystack, $strict)) { echo "$needle\n"; } ?>
5.6
5
<? $haystack = array(array("p", "h", "p"), array("o", "s", "b"), "o"); $needle = array("p", "h", "p"); if(in_array($needle, $haystack)) { echo "php\n"; } $needle = array("n", "e", "e", "d", "l", "e"); if(in_array($needle, $haystack)) { echo "needle\n"; } $needle = "o"; if(in_array($needle, $haystack)) { echo "o\n"; } ?>
php o
6
<? $haystack = array( "one" => null, "two" => false, "three" => true, "four" => 10, "five" => "20" ); $strict = true; echo "loose:\n"; echo in_array(null, $haystack); echo in_array(false, $haystack); echo in_array(true, $haystack); echo in_array(10, $haystack); echo in_array("20", $haystack); echo " "; echo in_array("zero", $haystack); echo in_array("one", $haystack); echo in_array("10", $haystack); echo in_array(20, $haystack); echo in_array(array(), $haystack); echo "\n\nstrict:\n"; echo in_array(null, $haystack, $strict); echo in_array(false, $haystack, $strict); echo in_array(true, $haystack, $strict); echo in_array(10, $haystack, $strict); echo in_array("20", $haystack, $strict); echo " "; echo in_array("zero", $haystack, $strict); echo in_array("one", $haystack, $strict); echo in_array("10", $haystack, $strict); echo in_array(20, $haystack, $strict); echo in_array(array(), $haystack, $strict); ?>
loose: 11111 11111 strict: 11111
Links
Array
- array
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort