2017-07-20 95 views
0

刪除使用特定密鑰重複我具有以下陣列合併陣列和PHP

array:3 [ 
    0 => array:3 [ 
    0 => "EN" 
    1 => "ENGLISH" 
    2 => 1 
    ] 
    1 => array:3 [ 
    0 => "JA" 
    1 => "JAPANESE" 
    2 => 1 
    ] 
    2 => array:3 [ 
    0 => "JA" 
    1 => "JAPANESE" 
    2 => 0 
    ] 
] 

欲刪除重複但只檢查鍵和。 當我使用array_unique()它不起作用。

我希望得到的結果是

array:2 [ 
     0 => array:3 [ 
     0 => "EN" 
     1 => "ENGLISH" 
     2 => 1 
     ] 
     1 => array:3 [ 
     0 => "JA" 
     1 => "JAPANESE" 
     2 => 1 
     ] 
    ] 

最後一個數組被刪除,因爲它有具有保持不變JAJAPANESE,和一個。

在此先感謝。使用

回答

0

您使用使用唯一索引爲重點做到這一點,

$result = []; 
foreach($array as $v) 
{ 
    $result[$v[0] . $v[1]] = $v; 
} 
$result = array_values($result); 
+1

它的工作原理!非常感謝 :) –

0

收集可以通過

// using collection 
$collection = collect([ ["EN", "ENGLISH", 1],["JP", "JAPAN", 1], ["JP", "JAPAN", 1] ]); 

// then filtering the value 
$filtered = $collection->filter(function ($value, $key) { 
    return $value[2] == 1; 
}); 

// then unique only by the acronym (en , jp) 
$unique = $filtered->unique(0); 

// you may also add the 2nd value to determine it's uniqueness 
$unique = $filtered->unique(function ($item) { 
    return $item[0].$item[1]; 
}); 

// getting all the uniqued values 
$unique->values()->all();