2014-12-23 72 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

是的,接受的答案足以满足要求,但我只是试图提供一个更抽象的解决方案,可以在更多的情况下使用:) –