2014-12-23 100 views
0

我有這樣PHP數組排序,並通過兩場刪除重複值

[0]=>array(3) { 
    ["Number"]=> "L1" 
    ["Location"]=> "Location-A" 
    ["Qty"]=>"1" 
    } 
    [1]=>array(3) { 
    ["Number"]=> "L1" 
    ["Location"]=> "Location-B" 
    ["Qty"]=> "5" 
    } 
    [2]=> array(3) { 
    ["Number"]=> "L1" 
    ["Location"]=> "Location-B" 
    ["Qty"]=> "4" 
    } 
    [3]=>array(3) { 
    ["Number"]=> "L2" 
    ["Location"]=> "Location-B" 
    ["Qty"]=> "5" 
    } 

的陣列結構,但我需要以下的結構輸出中

[0]=>array(3) { 
    ["Number"]=> "L1" 
    ["Location"]=> "Location-A" 
    ["Qty"]=>"1" 
    } 
    [1]=> array(3) { 
    ["Number"]=> "L1" 
    ["Location"]=> "Location-B" 
    ["Qty"]=> "4" 
    } 
    [2]=>array(3) { 
    ["Number"]=> "L2" 
    ["Location"]=> "Location-B" 
    ["Qty"]=> "5" 
    } 

我如何可以按編號刪除重複值,位置?

ksort只適用於一個值,我需要刪除兩個值,我怎麼能實現這個PHP?

$ordered = array(); 
foreach ($data as $da) 
{   
    $ordered[$da['Number']] = $da; 
    $ordered[$da['Location']] = $da;    
} 
ksort($ordered); 
+0

你怎麼知道保留哪一個? – RST

+0

創建一個新數組:array_tmp(),並使用'array_push()'插入並比較數字和位置。 – EngineerCoder

+0

這個問題是鬆散版本的http://stackoverflow.com/questions/27526145/delete-duplicate-on-multidimensional-array-and-take-those-having-highest-value-i – axiac

回答

1

創建新陣列時,串聯的兩個領域:

foreach ($data as $da) { 
    $result[$da['Number'] . '.' . $da['Location']] = $da; 
} 
$result = array_values($result); // Turn it back into indexed array 
+1

它保持最後一個值每個'Number'和''Location'對都有'$ data'。由於OP沒有提及應該爲重複項保留哪個值,所以該解決方案是可以的。 – axiac

1

試試這個..

<?php 
    $array = array(
     0 => array('Number'=>'L1','Location'=>'Location-A','Qty'=>'1'), 
     1 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'5'), 
     2 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'4'), 
     3 => array('Number'=>'L2','Location'=>'Location-B','Qty'=>'5'), 
    ); 
    $output = array_values(array_intersect_key($array,array_unique(array_map(function($arrayval) { 
     return $arrayval['Number'] . '.' .$arrayval['Location']; 
    }, $array)) 
)); 
    print_r($output); 

輸出

Array ([0] => Array ([Number] => L1 [Location] => Location-A [Qty] => 1) 
     [1] => Array ([Number] => L1 [Location] => Location-B [Qty] => 5) 
     [2] => Array ([Number] => L2 [Location] => Location-B [Qty] => 5)) 
+0

當你在'Qty'上調用'array_unique'時,它如何返回唯一的'Number'和'Location'? – Barmar

+0

你的結果有兩個元素,Number => L1,Location => Location-B。 – Barmar

+0

'array_intersect_key()','array_unique()'和'array_map()':循環遍歷數組3次,而一次傳遞就足夠了。不好。 – axiac

0

試試這個:

function array_unique_c($array, Closure $comparer) { 
    $result = array(); 
    for($i = 0; $i < count($array); $i++) { 
     $duplicates = false; 
     for($n = $i + 1; $n < count($array); $n++) { 
      if ($comparer($array[$i], $array[$n])) { 
       $duplicates = true; 
       break; 
      } 
     } 
     if(!$duplicates) { 
      $result[] = $array[$i]; 
     } 
    } 

    return $result; 
} 

用法:

$uniqueArray = array_unique_c($a, function ($itemA, $itemB) { 
    return $itemA['Number'] == $itemB['Number'] && $itemA['Location'] == $itemB['Location']; 
}); 

輸出:

array(3) { 
    [0] => array(3) { 
     ["Number"] => string(2) "L1" 
     ["Location"] => string(10) "Location-A" 
     ["Qty"] => string(1) "1" 
    } 
    [1] => array(3) { 
     ["Number"]=> string(2) "L1" 
     ["Location"]=> string(10) "Location-B" 
     ["Qty"]=> string(1) "4" 
    } 
    [2]=> array(3) { 
     ["Number"]=> string(2) "L2" 
     ["Location"]=> string(10) "Location-B" 
     ["Qty"]=> string(1) "5" 
    } 
} 
+0

代碼太複雜(難以理解),由於內部循環,性能很差。通過數組的單個循環足以滿足請求。 – axiac

+0

是的,接受的答案足以滿足要求,但我只是試圖提供一個更抽象的解決方案,可以在更多的情況下使用:) –