0

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.

Naveenbos
  • 2,396
  • 3
  • 31
  • 58
  • and WHAT is your isssue? What should be sorted in which order? ... – donald123 Mar 25 '20 at 11:58
  • It should be sorted by Distance – Naveenbos Mar 25 '20 at 11:59
  • Which is not yet solved – Naveenbos Mar 25 '20 at 12:05
  • @donald123 it should sort by distance with out changing the sort order values. – Naveenbos Mar 25 '20 at 12:16
  • @mickmackusa I have tried this below code array_multisort(array_map(function($element) { return $element['Distance']; }, $results), SORT_ASC, $results); and it is sorted with distance but the sort order values order changed, actually the array should be sorted with distance but, it should not change the sort order [1] – Naveenbos Mar 25 '20 at 13:03
  • @mickmackusa which returns nothing. – Naveenbos Mar 25 '20 at 13:38
  • @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. – Naveenbos Mar 25 '20 at 13:52
  • @mickmackusa Your given code is working with your example array. when I use my question array it is not returns anything, can you please try with my question array. – Naveenbos Mar 25 '20 at 16:39
  • @mickmackusa I am using php 5.3.3 does it cause any issue. – Naveenbos Mar 25 '20 at 16:50
  • 1
    https://3v4l.org/gn727 @Nav – mickmackusa Mar 25 '20 at 22:09
  • @mickmackusa It is working perfectly. Can you please add this as an answer so I can give Upvote and accepted answer. Thank You for the help. – Naveenbos Mar 26 '20 at 06:16
  • 1
    No can do. This page is closed and there is no hope of reopening it. I helped you full-well knowing that there was no rep reward in store for me. I am happy that you can move forward. I volunteer because I enjoy helping people, not for the magical unicorn points. Happy coding. – mickmackusa Mar 26 '20 at 06:17
  • @mickmackusa I just edit the question and add answer. Thanks – Naveenbos Mar 26 '20 at 06:51
  • I am sorry to tell you that this page will not have a long lifespan so it is futile to try to improve the question. All php sorting questions are duplicates and the system/community will delete this page in a matter of days. You should really update your server asap. – mickmackusa Mar 26 '20 at 07:03

0 Answers0