如何确定关联数组是否是另一个关联数组的子集


How to determine if an associative array is a subset of another associative array

我有一个关于确定包含另一个数组索引的 PHP 关联数组是否是另一个类似构造的关联数组(即包含另一个数组索引的数组(的子集的问题。

假设我有两个数组,一个命名为,另一个命名为腿动物

<?php 
$dogs = array(0 => array('height' => 100, 'weight' => 100), 
              1 => array('height' => 50, 'weight' => 50));
$legged-animals = array(0 => array('height' => 200, 'weight' => 500), 
                        1 => array('height' => 220, 'weight' => 500), 
                        2 => array('height' => 100, 'weight' => 100), 
                        3 => array('height' => 50, 'weight' => 50));
?>

所以问题是,我如何确定有腿动物的一个子集?

编辑:以下是我试图找出一个是否是另一个的子集的尝试:

function filter($largeSets, $subSets)
{
    $result = array();
    $count = count($subSets);
    foreach ($largeSets as $individualSet)
    {
        foreach ($subSets as $set)
        {
            $intersection = array_intersect($individualSet, $set);
            if (!empty($intersection) && isset($intersection['height']) && isset($intersection['weight']))
            {
                $result['array'][] = $individualSet;
                $count--;
                break;
            }
        }
    }
    $result['result'] = ($count == 0);
    return $result;
}

更新:这是一个更简单的解决方案,我认为可以解决问题。这个想法是遍历多维数组,serialize数组,然后使用 array_intersect .

$dogs = array(0 => array('height' => 100, 'weight' => 100), 
              1 => array('height' => 50, 'weight' => 50),
              2 => array('height' => 10, 'weight' => 25) );
$legged_animals = array(0 => array('height' => 200, 'weight' => 500), 
                        1 => array('height' => 220, 'weight' => 500), 
                        2 => array('height' => 100, 'weight' => 100), 
                        3 => array('height' => 50, 'weight' => 50));
foreach ($dogs as $dog)
{
    $arr[] = serialize($dog);
}  
foreach ($legged_animals as $animal)
{
    $arr2[]  = serialize($animal);
}
$intersection = array_intersect($arr, $arr2);
print_r($intersection);

此时,intersection将打印出交叉点的序列化形式。要获得初始结果,您必须unserialize数组。

有没有更简单的方法可以做到这一点?

这个冗长的方法怎么样:

// borrowed from giosh at http://php.net/manual/en/function.array-diff-assoc.php
function array_diff_assoc_recursive($array1, $array2) {
    $difference=array();
    foreach($array1 as $key => $value) {
        if( is_array($value) ) {
            if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
                $difference[$key] = $value;
            } else {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if( !empty($new_diff) )
                    $difference[$key] = $new_diff;
            }
        } else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
            $difference[$key] = $value;
        }
    }
    return $difference;
}
function array_is_sub($needle,$haystack)
{
  foreach ($needle as $n)
  {  
    $matched=false;
    foreach ($haystack as $h)
    {
      if (empty(array_diff_assoc_recursive($n, $h)))
      { 
         $matched=true;
         break;
      }
    }
    if (!$matched) return false;
  }
  return true;
}
$dogs = array(0 => array('height' => 100, 'weight' => 100), 
              1 => array('height' => 50, 'weight' => 50));
$legged_animals = array(0 => array('height' => 200, 'weight' => 500), 
                        1 => array('height' => 220, 'weight' => 500), 
                        2 => array('height' => 100, 'weight' => 100), 
                        3 => array('height' => 50, 'weight' => 50));
if (array_is_sub($dogs,$legged_animals)) {
  echo "Dogs is a subset of Legged_Animals.";
} else {
  echo "Dogs is NOT a subset of Legged_Animals.";
}

应该工作。

这个解决方案非常适合我需要的:

// takes two multidimensional arrays, $arr1 and $arr2
// and returns the intersection of the two 
// @return an array that is the intersection of the two
function findIntersection($arr1, $arr2)
{
    $retArray = array();
    $firstArray = array();
    $secondArray = array();
    $intersection = array();
    foreach ($arr1 as $set)
    {
        $firstArray[] = serialize($set);
    }
    foreach ($arr2 as $set)
    {
        $secondArray[] = serialize($set);
    }
    $intersection = array_intersect($firstArray, $secondArray);
    if (!empty($intersection))
    {
        foreach ($intersection as $serializedArray)
        {
            $retArray[] = unserialize($serializedArray);
        }
    }
    return $retArray;
}

array_intersect(( 可以解决问题(不要在数组名称中使用破折号(:

$dogs = array(0 => array('height' => 100, 'weight' => 100), 
              1 => array('height' => 50, 'weight' => 50));
$leggedanimals = array(0 => array('height' => 200, 'weight' => 500), 
                        1 => array('height' => 220, 'weight' => 500), 
                        2 => array('height' => 100, 'weight' => 100), 
                        3 => array('height' => 50, 'weight' => 50));
print_r( array_intersect( $dogs, $leggedanimals ) );
// Array ( [0] => Array ( [height] => 100 [weight] => 100 ) [1] => Array ( [height] => 50 [weight] => 50 ) )