in_array

(PHP 4, PHP 5, PHP 7, PHP 8)

in_array检查数组中是否存在某个值

说明

in_array(mixed $needle, array $haystack, bool $strict = false): bool

大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。

参数

needle

待搜索的值。

注意:

如果 needle 是字符串,则比较是区分大小写的。

haystack

待搜索的数组。

strict

如果第三个参数 strict 的值为 truein_array() 函数还会检查 needle类型是否和 haystack 中的相同。

注意:

在 PHP 8.0.0 之前,string needle 在非严格模式下将会匹配数组中的值 0,反之亦然。这可能会导致不希望的结果。其它类型也存在类似的边缘情况。如果不是绝对确定有关值的类型,请始终使用 strict flag 以避免意外行为。

返回值

如果找到 needle 则返回 true,否则返回 false

示例

示例 #1 in_array() 例子

<?php
$os
= array("Mac", "NT", "Irix", "Linux");
if (
in_array("Irix", $os)) {
echo
"Got Irix";
}
if (
in_array("mac", $os)) {
echo
"Got mac";
}
?>

第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:

Got Irix

示例 #2 in_array() 严格类型检查例子

<?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";
}
?>

以上示例会输出:

1.13 found with strict check

示例 #3 in_array() 中用数组作为 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";
}
?>

以上示例会输出:

'ph' was found
  'o' was found

参见

  • array_search() - 在数组中搜索给定的值,如果成功则返回首个相应的键名
  • isset() - 检测变量是否已声明并且其值不为 null
  • array_key_exists() - 检查数组里是否有指定的键名或索引

添加备注

用户贡献的备注 5 notes

up
411
beingmrkenny at gmail dot com
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

// Example array

$array = array(
'egg' => true,
'cheese' => false,
'hair' => 765,
'goblins' => null,
'ogres' => 'no ogres allowed in this array'
);

// Loose checking -- return values are in comments

// First three make sense, last four do not

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

// Strict checking

in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false

?>
up
3
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
];

// Let's try to find number 7 within $array
$hasNumber = in_array(7, $array, true); // bool(false)
$hasNumber = in_array_recursive(7, $array, true); // bool(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;
}
up
10
rhill at xenu-directory dot net
15 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';
// 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.
up
1
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(); // it gets generated/fetched by another service, so I don't know what value it will have. P.S.: it's an integer

// The actual data in my edge-case scenario:
// $someId = 0;
// $anyArray = ['dataOne', 'dataTwo'];
if (in_array($someId, $anyArray)) {
// do some work
}
// ...
?>

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.
up
-4
Armands Rieksti
1 year ago
I'd like to point out that, if you're using Enum data structures and want to compare whether an array of strings has a certain string Enum in it, you need to cast it to a string.

From what I've tested, the function works correctly:
if the array is filled with strings and you're searching for a string;
if the array is filled with Enums and you're searching for an Enum.
To Top