2015-09-07 255 views
1

這是我的2個陣列:PHP - 比較兩個數組

Array 
(
[25] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

[26] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

[36] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 
      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

) 

Array 
(
[25] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

[26] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

[30] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

) 

,現在我想比較這些陣列,並返回其包含在兩個數組所有arrayitems。在這個例子中是[25]和[26]。

,返回的數組應該是這樣的(正如我所說[25]和[26]都包含在兩個數組所以其返回):

Array 
(
[25] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 

[26] => Array 
    (
     [items_to_give] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

     [items_to_get] => Array 
      (
       [0] => Array 
        (
         [xx] => xx 
         [xx] => xx 
        ) 

      ) 

    ) 
) 

什麼我已經嘗試過:

$result = array_map("unserialize", array_intersect($this->serialize_array_values($array1),$this->serialize_array_values($array2))); 

function serialize_array_values($arr){ 
    foreach($arr as $key=>$val){ 
     sort($val); 
     $arr[$key]=serialize($val); 
    } 

    return $arr; 
} 

但是這會返回錯誤格式的數組。 [items_to_give]和[items_to_get]正被轉換爲索引[0]和[1],這是不可取的。

我想數組停留,因爲他是返回時,這意味着我需要[items_to_give]和[items_to_get]而不是索引[0]和[1]

什麼是其他可能的解決方案?感謝解決方案。

+1

嘗試'和array_diff()' –

+1

array_intersect()和array_intersect_assoc() –

+1

聽起來你正在尋找[array_intersect_key](http://php.net/manual/ en/function.array-intersect-key.php) – billyonecan

回答

1

試試這個功能:

function my_array_diff($arr1, $arr2) 
    { 
    $res = array(); 
    foreach ($arr1 as $val1) { 
     $val1 = serialize($val1); 
     foreach ($arr2 as $key2 => $val2) { 
     if (serialize($val2) == $val1) { 
      $res[$key2] = $val2; 
     } 
     } 
    } 

    return $res; 
    } 
+0

作品完美!非常感謝。 – lhuber