2014-12-07 100 views
-1

假設所有這些數據都具有相同的數據。如何在這個多維數組中執行此操作。檢查兩個以上的數組是否具有相同的數據

基本上我嘗試使用

array_intersect($array,$array); 

找到所有相同的元素,但我想不出我應該怎樣才能做這樣做。

array(14) { 
    [0]=> 
    string(5) "0.1" 
    [1]=> 
    string(5) "0.2" 
    [2]=> 
    string(5) "0.3" 
    [3]=> 
    string(5) "0.4" 
    [4]=> 
    string(5) "0.1" 
    [5]=> 
    string(5) "0.2" 
    [6]=> 
    string(5) "0.3" 
    [7]=> 
    string(5) "0.4" 
    [8]=> 
    string(5) "0.2" 
    [9]=> 
    string(5) "0.3" 
    [10]=> 
    string(5) "0.4" 
    [11]=> 
    string(5) "0.1" 
    [12]=> 
    string(5) "0.2" 
    [13]=> 
    string(5) "0.3" 
    } 
+0

陣列(14){ [0] => 串(5) 「0.1」 [1] => 串(5) 「0.2」 [2] => 串(5) 「0.3」 [3] => 串(5) 「0.4」 [4] => 串(5) 「0.1」 [5] => 串(5) 「0.2」 [6] => 串(5) 「0.3」 [7] => 串(5) 「0.4」 [8] => 串(5) 「0.2」 [ 9] => 串(5) 「0.3」 [10] => 串(5) 「0.4」 [11] => 串(5) 「0.1」 [12] => 串(5 )「0.2」 [13] => 字符串(5)「0.3」 } 我試圖找出什麼是所有這些共同點。這就是我做var_dump($ compare) – TheCryptKeeper 2014-12-07 20:47:27

+0

後得到的結果,輸入與第一個問題中的輸入完全不同...刪除我的答案;)對於當前輸入,您不能執行zilch,因爲沒有第二個數據點進行檢查。你現在可以從輸入中得到的最多是'array_count_values',但是這仍然沒有告訴你數據點有什麼共同點。 – Wrikken 2014-12-07 20:59:05

+0

可以說我知道有4個數據集,我可以看到有多少次這個值出現4次。那麼array_count_values會完成那個嗎? – TheCryptKeeper 2014-12-07 21:09:47

回答

0

不知道你是否完全尋找:

$i=0; // index of the orginal array 
$common = array_reduce($array, function ($c, $v) use (&$i) { 
    foreach ($v['data'] as $nb) { 
     $c[$nb][] = $i; // store the index for the current number 
    } 
    $i++; // next index 
    return $c; 
}, array()); 

// remove the unique values 
$common = array_filter($common, function($item) { return count($item)>1; }); 

// sort by decreasing number of indexes 
uasort($common, function ($a, $b) {return count($b) - count($a);}); 

print_r($common); 

這將產生一個關聯數組與普通號碼作爲關鍵,並與原來的數組值的索引數組:

Array 
(
    [5] => Array (
      [0] => 0 
      [1] => 1 
      [2] => 2 
      [3] => 3 
     ) 
    [4] => Array (
      [0] => 0 
      [1] => 1 
      [2] => 3 
     ) 
    [2] => Array (
      [0] => 0 
      [1] => 1 
      [2] => 3 
     ) 
    [6] => Array (
      [0] => 1 
      [1] => 3 
     ) 
    [1] => Array (
      [0] => 1 
      [1] => 3 
     ) 
    [3] => Array (
      [0] => 1 
      [1] => 3 
     ) 
) 
相關問題