'PHP array sort and remove duplicates by two field values
I have an array structure like this
[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"
}
But i required below structure as ouput
[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"
}
How can i remove duplicate value by Number and Location?
ksort only works for one value, i need to remove by two values , how can i achieve this PHP ?
$ordered = array();
foreach ($data as $da)
{
$ordered[$da['Number']] = $da;
$ordered[$da['Location']] = $da;
}
ksort($ordered);
Solution 1:[1]
Concatenate the two fields when creating your new array:
foreach ($data as $da) {
$result[$da['Number'] . '.' . $da['Location']] = $da;
}
$result = array_values($result); // Turn it back into indexed array
Solution 2:[2]
Try this..
<?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);
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 ) )
Solution 3:[3]
Try this:
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;
}
Usage:
$uniqueArray = array_unique_c($a, function ($itemA, $itemB) {
return $itemA['Number'] == $itemB['Number'] && $itemA['Location'] == $itemB['Location'];
});
Output:
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"
}
}
Solution 4:[4]
Easy way to do this job:
$data = [
["Number"=> "L1","Location"=> "Location-A","Qty"=>"1"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"6"],
["Number"=> "L3","Location"=> "Location-A","Qty"=>"8"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"5"],
["Number"=> "L3","Location"=> "Location-A","Qty"=>"2"],
["Number"=> "L1","Location"=> "Location-B","Qty"=>"4"],
["Number"=> "L1","Location"=> "Location-B","Qty"=>"1"],
["Number"=> "L2","Location"=> "Location-B","Qty"=>"3"],
];
foreach ($data as $k=>$v) {
$arr[md5($v['Number'].$v['Location'])] = $v;
}
$result = array_values($arr); //can be omitted
As you can see $arr is equal $result and you can ommit array_values() func
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barmar |
Solution 2 | |
Solution 3 | Vytautas Lozickas |
Solution 4 | Arthur Veselov |