我使用in_array()来检查一个值是否存在于如下数组中,

$a = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $a)) 
{
    echo "Got Irix";
}

//print_r($a);

但是对于一个多维数组(下面)——我如何检查这个值是否存在于多数组中?

$b = array(array("Mac", "NT"), array("Irix", "Linux"));

print_r($b);

或者我不应该使用in_array()当涉及到多维数组?


当前回答

我相信你现在可以使用array_key_exists:

<?php
$a=array("Mac"=>"NT","Irix"=>"Linux");
if (array_key_exists("Mac",$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

其他回答

这样就可以了:

foreach($b as $value)
{
    if(in_array("Irix", $value, true))
    {
        echo "Got Irix";
    }
}

In_array仅对一维数组起作用,因此需要遍历每个子数组并在每个子数组上运行In_array。

正如其他人所注意到的,这只适用于二维数组。如果有更多嵌套数组,递归版本会更好。有关例子,请参阅其他答案。

自PHP 5.6以来,原来的答案有一个更好和更干净的解决方案:

使用这样的多维数组:

$a = array(array("Mac", "NT"), array("Irix", "Linux"))

我们可以使用splat操作符:

return in_array("Irix", array_merge(...$a), true)

如果你有这样的字符串键:

$a = array("a" => array("Mac", "NT"), "b" => array("Irix", "Linux"))

你将不得不使用array_values,以避免错误不能解包数组字符串键:

return in_array("Irix", array_merge(...array_values($a)), true)

In_array()在多维数组上不起作用。你可以写一个递归函数来做这件事:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

用法:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

jwueller所接受的解决方案(在撰写本文时)

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

完全正确,但在进行弱比较(参数$strict = false)时可能会出现意外行为。

由于PHP在比较不同类型的值时的类型杂耍

"example" == 0

and

0 == "example"

计算值为true,因为"example"被强制转换为int并转换为0。

(参见为什么PHP认为0等于一个字符串?)

如果这不是理想的行为,在进行非严格比较之前,可以方便地将数值转换为字符串:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {

        if( ! $strict && is_string( $needle ) && ( is_float( $item ) || is_int( $item ) ) ) {
            $item = (string)$item;
        }

        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

许多这样的搜索通常是为了在记录列表中查找内容,正如一些人指出的那样,这实际上是一个二维数组。

这适用于具有统一键集的记录列表),例如从数据库中获取的记录列表。

为了完整起见,这个结构包含了'in_array'和'key_exists'样式函数。两个函数都返回一个简单的true/false布尔值。

2维记录数组…

$records array:

  [0] => Array
    (
        [first_name] => Charlie
        [last_name] => Brown
    )
  [1] => Array
    (
        [first_name] => Fred
        [last_name] => Sanford
    )

功能:

function in_multidimensional_array($array, $column_key, $search) { 
   return in_array($search, array_column($array, $column_key)); 
}

function multidimensional_array_key_exists($array, $column_key) { 
   return in_array($column_key, array_keys(array_shift($array))); 
}

测试:

var_dump(in_multidimensional_array($records, 'first_name', 'Charlie')); // true

var_dump(multidimensional_array_key_exists($records, 'first_name')); // true