2014-01-09 47 views
0

我想要執行以下操作。將數組與多維數組進行比較以查找多餘元素

假設我有一個數組。

陣列A,

-> [0] = A 
-> [1] = B 
-> [2] = C 

,我想比較多維數組

-> [0] 
     [0] = 1 
     [1] = A 
     [2] = B 
     [3] = C 
-> [1] 
     [0] = 2 
     [1] = B 
     [2] = C 
     [3] = A 

-> [2] 
     [0] = 3 
     [1] = C 
     [2] = B 
     [3] = A 

我想第一陣列相比,在多維數組中的內容。正如你所看到的多維數組有一個數字附加到每個字母的組合。我想以這樣的方式進行比較,即我可以從數組B中獲取數組A的相應數量,假設它們也在數組B中。我怎樣才能做到這一點?

+0

「相應的數組A的數出數組b」你能給位更多的細節? –

+0

我想知道什麼編號屬於數組A.因此,例如,我希望比較返回1在這種情況下。我想要數組B的數量與陣列A有相同的元素。 – MichaelP

回答

2

這應做到:

public function findSubArray($multiArr, $compArr) { 
    foreach ($multiArr as $idx => $arr) { //for each sub array 
    $first = array_shift($arr); //take off the first element and store it 
    if (count(array_diff($arr, $compArr)) == 0) { //check if the remainder is what you want 
     return $first; //if so, return the first element 
    } 
    } //otherwise loop 
} 
+1

這正是我想要實現的。你讓它看起來很簡單!謝謝一堆! – MichaelP