我使用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()当涉及到多维数组?


当前回答

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

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

为了完整起见,这个结构包含了'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

其他回答

那么array_search呢?根据https://gist.github.com/Ocramius/1290076的说法,似乎比foreach快得多。

if( array_search("Irix", $a) === true) 
{
    echo "Got Irix";
}

我发现下面的解决方案不是很干净的代码,但它的工作。它被用作递归函数。

function in_array_multi( $needle, $array, $strict = false ) {
  foreach( $array as $value ) { // Loop thorugh all values
    // Check if value is aswell an array
    if( is_array( $value )) {
      // Recursive use of this function
      if(in_array_multi( $needle, $value )) {
        return true; // Break loop and return true
      }
    } else {
      // Check if value is equal to needle
      if( $strict === true ) {
        if(strtolower($value) === strtolower($needle)) {
          return true; // Break loop and return true
        }
      }else {
        if(strtolower($value) == strtolower($needle)) {
          return true; // Break loop and return true
        }
      }
    }
  }

  return false; // Nothing found, false
}

如果你的数组像这样

$array = array(
              array("name" => "Robert", "Age" => "22", "Place" => "TN"), 
              array("name" => "Henry", "Age" => "21", "Place" => "TVL")
         );

使用这个

function in_multiarray($elem, $array,$field)
{
    $top = sizeof($array) - 1;
    $bottom = 0;
    while($bottom <= $top)
    {
        if($array[$bottom][$field] == $elem)
            return true;
        else 
            if(is_array($array[$bottom][$field]))
                if(in_multiarray($elem, ($array[$bottom][$field])))
                    return true;

        $bottom++;
    }        
    return false;
}

示例:echo in_multiarray("22", $array,"Age");

这是我在in_array的php手册中找到的第一个此类函数。评论区的函数并不总是最好的,但如果它不能做到这一点,你也可以在那里看看:)

<?php
function in_multiarray($elem, $array)
    {
        // if the $array is an array or is an object
         if( is_array( $array ) || is_object( $array ) )
         {
             // if $elem is in $array object
             if( is_object( $array ) )
             {
                 $temp_array = get_object_vars( $array );
                 if( in_array( $elem, $temp_array ) )
                     return TRUE;
             }

             // if $elem is in $array return true
             if( is_array( $array ) && in_array( $elem, $array ) )
                 return TRUE;


             // if $elem isn't in $array, then check foreach element
             foreach( $array as $array_element )
             {
                 // if $array_element is an array or is an object call the in_multiarray function to this element
                 // if in_multiarray returns TRUE, than return is in array, else check next element
                 if( ( is_array( $array_element ) || is_object( $array_element ) ) && $this->in_multiarray( $elem, $array_element ) )
                 {
                     return TRUE;
                     exit;
                 }
             }
         }

         // if isn't in array return FALSE
         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