2015-11-18 65 views
1

我已經搜索並嘗試了許多不同的「從數組中刪除重複」功能,但沒有人爲我的情況制定出來。我試圖從數組中刪除一個特定的重複。從數組中刪除特定的重複鍵值

從下面我想刪除「PHASER 4600」的重複。

[0] => Array 
    (
     [id] => 1737 
     [product_name] => PHASER 4200 
     [certification_date] => 3/20/12 
    ) 

[1] => Array 
    (
     [id] => 1738 
     [product_name] => PHASER 4600 
     [certification_date] => 3/20/12 
    ) 

[2] => Array 
    (
     [id] => 1739 
     [product_name] => PHASER 4600 
     [certification_date] => 3/20/12 
    ) 

[3] => Array 
    (
     [id] => 1740 
     [product_name] => PHASER 4700 
     [certification_date] => 3/20/12 
    ) 

[4] => Array 
    (
     [id] => 1741 
     [product_name] => PHASER 4800 
     [certification_date] => 3/20/12 
    ) 
+0

你說你已經嘗試了各種東西,但不顯示你的工作。請添加一些代碼,以便我們提供幫助。關於這個問題,[array_filter()](http://php.net/array_filter)可能會有所幫助。 –

+0

從哪裏得到這個數組 – kannan

+0

我相信這是awnsered [這裏](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in -php) – Mazaka

回答

1

你可以把它們放到一個新的數組中,並在你放入它們時檢查它是否是重複的。

$newArray = array(); 

foreach ($oldArray as $old) { 
    $found = false; 

    foreach ($newArray as $new) { 
     if ($new['product_name'] == $old['product_name']) { 
      $found = true; 
     } 
    } 

    if (!$found) { 
     array_push($newArray, $old); 
    } 
} 
1

您可以使用此功能:

function delete_duplicate_name(&$arr, $name){ 
    $found = false; 
    foreach($arr as $key => $elm){ 
     if($elm['product_name'] == $name){ 
      if($found == true) 
       unset($arr[$key]); 
      else 
       $found = true; 
     } 
    } 
} 
delete_duplicate_name($arr, 'PHASER 4600'); 
print_r($arr);