2013-10-09 84 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; 
} 
相關問題