2013-10-09 114 views
0

我有一个关于确定一个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阵列。

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

+0

array_intersect()+计数()? –

+0

你能多解释一下吗?谢谢! –

+0

@ZhiaChong - 浏览子集。如果超集中的所有内容都很好,否则它不是子集。记得维恩图? –

回答

-1

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)) 
+0

'array_intersect()'确实不适用于像这样的多维数组。您需要首先序列化每个内部数组,然后再进行数组交集。 –

0

这个怎么样长篇大论方法:

// 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."; 
} 

应该工作。

+0

我是否理解你想要一个真正的,如果狗是legged_animal的一个子集,否则为false? –

0

这个解决方案工作完全正常了什么,我需要:

// 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; 
} 
相关问题