My result is showing like this on Php. It should be sorted by Distance.
Array
(
[0] => Array
(
[CompanyID] => 46489
[Brand] => First Brand name
[Name] => First Company name
[Address] => First Address
[Distance] => 9.67823446552613
[Group] => S
[SortOrder] => 1
)
[1] => Array
(
[CompanyID] => 46490
[Brand] => Second Brand name
[Name] => Secod Company name
[Address] => Second Address
[Distance] => 8.67823446552613
[Group] => S
[SortOrder] => 1
)
[2] => Array
(
[CompanyID] => 46491
[Brand] => Third Brand name
[Name] => Third Company name
[Address] => Third Address
[Distance] => 2.17623213863985
[Group] => S
[SortOrder] => 1
)
[3] => Array
(
[CompanyID] => 46492
[Brand] => Fourth Brand name
[Name] => Fourth Company name
[Address] => Fourth Address
[Distance] => 8.17623213863985
[Group] => G
[SortOrder] => 5
)
[4] => Array
(
[CompanyID] => 46493
[Brand] => Fifth Brand name
[Name] => Fifth Company name
[Address] => Fifth Address
[Distance] => 2.17623213863985
[Group] => G
[SortOrder] => 5
)
)
expected result is
Array
(
[0] => Array
(
[CompanyID] => 46491
[Brand] => Third Brand name
[Name] => Third Company name
[Address] => Third Address
[Distance] => 2.17623213863985
[Group] => S
[SortOrder] => 1
)
[1] => Array
(
[CompanyID] => 46490
[Brand] => Second Brand name
[Name] => Secod Company name
[Address] => Second Address
[Distance] => 8.67823446552613
[Group] => S
[SortOrder] => 1
)
[2] => Array
(
[CompanyID] => 46489
[Brand] => First Brand name
[Name] => First Company name
[Address] => First Address
[Distance] => 9.67823446552613
[Group] => S
[SortOrder] => 1
)
[3] => Array
(
[CompanyID] => 46493
[Brand] => Fifth Brand name
[Name] => Fifth Company name
[Address] => Fifth Address
[Distance] => 2.17623213863985
[Group] => G
[SortOrder] => 5
)
[4] => Array
(
[CompanyID] => 46492
[Brand] => Fourth Brand name
[Name] => Fourth Company name
[Address] => Fourth Address
[Distance] => 8.17623213863985
[Group] => G
[SortOrder] => 5
)
)
I tried different Sorting methods but which could not solve My issue. I do not want to change the sort order and the group, Any help is appreciated.
I have tried the below code
array_multisort(array_map(function($element) {
return $element['Distance'];
}, $results), SORT_ASC, $results);
This code will sort the distance array by asc, but it is also changing the sort order values. THe expected result is shown above.
@mickmackusa There is another value called sort order, So I do not want ot change that, suppose there is a sort order value 1 have 3 product and 3 of them have different distance, similarly I have another sort order with 2 it has around 2 products, so I want to arrange this sortorder1 with distance, and sort order 2 with distance in the same array. $array = [
['CompanyID' => '46491','Brand' => 'Fourth2 Brand name','Distance' => '9.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46499','Brand' => 'Fourth3 Brand name','Distance' => '2.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46422','Brand' => 'Fourth1 Brand name','Distance' => '4.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46492','Brand' => 'Fourth4 Brand name','Distance' => '8.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
['CompanyID' => '46493','Brand' => 'Fourth5 Brand name','Distance' => '5.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
]; I want to keep the sort order 1 in the first but needs to sort by Distance, in the same array.
I am posting the answer here since the question is closed by someone else before getting the solution.
If you are using PHP version 5.6 and above you can use the below code.
$array = [
['CompanyID' => '46491','Brand' => 'Fourth2 Brand name','Distance' => '9.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46422','Brand' => 'Fourth1 Brand name','Distance' => '4.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46492','Brand' => 'Fourth4 Brand name','Distance' => '8.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
['CompanyID' => '46499','Brand' => 'Fourth3 Brand name','Distance' => '2.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46493','Brand' => 'Fourth5 Brand name','Distance' => '5.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
];
uasort($array, 'sort_Distance');
function sort_Distance($a, $b) {
return [$a['SortOrder'], $a['Distance']] <=> [$b['SortOrder'], $b['Distance']];
}
var_export($array);
If you are using PHP version 5.3 and you can use the below code.
$results = [
['CompanyID' => '46491','Brand' => 'Fourth2 Brand name','Distance' => '9.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46422','Brand' => 'Fourth1 Brand name','Distance' => '4.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46492','Brand' => 'Fourth4 Brand name','Distance' => '8.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
['CompanyID' => '46499','Brand' => 'Fourth3 Brand name','Distance' => '2.67823446552613', 'Group'=>'S',"SortOrder"=>"1"],
['CompanyID' => '46493','Brand' => 'Fourth5 Brand name','Distance' => '5.67823446552613', 'Group'=>'G',"SortOrder"=>"2"],
];
array_multisort(
array_map(function($row) {
return $row['SortOrder'];
}, $results),
array_map(function($row) {
return $row['Distance'];
}, $results),
$results
);
var_export($results);
Thanks @mickmackusa for the answer.