(PHP 4, PHP 5, PHP 7, PHP 8)
in_array — Checks if a value exists in an array
Parametersneedle
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
.
Note:
Prior to PHP 8.0.0, a
string
needle
will match an array value of0
in non-strict mode, and vice versa. That may lead to undesireable results. Similar edge cases exist for other types, as well. If not absolutely certain of the types of values involved, always use thestrict
flag to avoid unexpected behavior.
Returns true
if needle
is found in the array, false
otherwise.
Example #1 in_array() example
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
The second condition fails because in_array() is case-sensitive, so the program above will display:
Example #2 in_array() with strict example
<?php
$a = array('1.10', 12.4, 1.13);
if (
in_array('12.4', $a, true)) {
echo "'12.4' found with strict check\n";
}
if (
in_array(1.13, $a, true)) {
echo "1.13 found with strict check\n";
}
?>
The above example will output:
1.13 found with strict check
Example #3 in_array() with an array as needle
<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (
in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (
in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (
in_array('o', $a)) {
echo "'o' was found\n";
}
?>
The above example will output:
'ph' was found 'o' was foundSee Also
13 years ago
Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.
The solution is to use the strict checking option.
<?php$array = array(
'egg' => true,
'cheese' => false,
'hair' => 765,
'goblins' => null,
'ogres' => 'no ogres allowed in this array'
);in_array(null, $array); in_array(false, $array); in_array(765, $array); in_array(763, $array); in_array('egg', $array); in_array('hhh', $array); in_array(array(), $array); in_array(null, $array, true); in_array(false, $array, true); in_array(765, $array, true); in_array(763, $array, true); in_array('egg', $array, true); in_array('hhh', $array, true); in_array(array(), $array, true); ?>
Julian Sawicki ¶
1 year ago
Here is a recursive in_array function:
<?php
$myNumbers
= [
[1,2,3,4,5],
[6,7,8,9,10],
];$array = [
'numbers' => $myNumbers
];$hasNumber = in_array(7, $array, true); $hasNumber = in_array_recursive(7, $array, true); function in_array_recursive(mixed $needle, array $haystack, bool $strict): bool
{
foreach ($haystack as $element) {
if ($element === $needle) {
return true;
}$isFound = false;
if (is_array($element)) {
$isFound = in_array_recursive($needle, $element, $strict);
}
if (
$isFound === true) {
return true;
}
}
return
false;
}
leonhard dot radonic+phpnet at gmail dot com ¶
2 years ago
I got an unexpected behavior working with in_array. I'm using following code:
<?php
$someId = getSomeId(); if (in_array($someId, $anyArray)) {
}
?>
With PHP7.4, in_array returns boolean true.
With PHP8.1, in_array returns boolean false.
It took me quite some time to find out what's going on.
rhill at xenu-directory dot net ¶
16 years ago
I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:
<?php
$needle
= array(
'fruit'=>'banana', 'vegetable'=>'carrot'
);$haystack = array(
array('vegetable'=>'carrot', 'fruit'=>'banana'),
array('fruit'=>'apple', 'vegetable'=>'celery')
);
echo
in_array($needle, $haystack, true) ? 'true' : 'false';
echo in_array($needle, $haystack) ? 'true' : 'false';
?>
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.
eurorusty at yahoo dot ca ¶
5 months ago
I'm not sure why PHP doesn't provide a way to specify a binary search. Here's an example of the performance gains, for this array size, about 50x improvement using interpreted PHP. If built in, it could probably achieve around 1000x improvement, again for this array size.
<?php$X = array(1);
for ($j = 1; $j < 50000; ++$j)
$X[] = $X[$j - 1] + rand(1, 6);$x = -microtime(true);
$m = 0;
for ($j = 0; $j < 10000; ++$j)
$m += in_array(rand(1, 175000), $X);
$x += microtime(true);
echo $x.PHP_EOL;$x = -microtime(true);
$m = 0;
for ($j = 0; $j < 10000; ++$j)
$m += binarySearch($X, rand(1, 175000));
$x += microtime(true);
echo $x.PHP_EOL;
function
binarySearch($array, $value) {
$low = 0;
$high = count($array) - 1;
while ($low <= $high) {
$pivot = floor(($low + $high) / 2);
if ($array[$pivot] == $value)
return true;
if ($value < $array[$pivot])
$high = $pivot - 1;
else
$low = $pivot + 1;
}
return false;
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3