0

I'm looking for a solution to sort by key a multi dimensional array, the point is that I can sort a set of array with multidimensional that has no custom key ID and the array is in set, but this array contains custom key and I need to sort by date:

My array is:

$newDataSet = array(
 '2017-02-03' => array(
  array(
    array(
     'name' => 'Paul',
     'state' => 'in',
     'date' => '2017-02-03'    
    ),
    array(
     'name' => 'Paul',
     'state' => 'out',
     'date' => '2017-02-03'    
    )
  )
 ),
'2017-01-02' => array(
  array(
    array(
     'name' => 'John',
     'state' => 'in',
     'date' => '2017-01-02'    
    ),
    array(
     'name' => 'John',
     'state' => 'out',
     'date' => '2017-01-02'    
    )
  )
 ),
'2017-04-01' => array(
  array(
    array(
     'name' => 'Smith',
     'state' => 'in',
     'date' => '2017-04-01'    
    ),
    array(
     'name' => 'Smith',
     'state' => 'out',
     'date' => '2017-04-01'    
    )
  )
 )
);

uasort($newDataSet, function($a, $b) { return($a['date'] - $b['date']); });

As I understand usort will sort only the array of the multidimensional, so in my case is wrong because I don't have the structure to use it, is there a way to order by key asc ? I mean by date asc ?

Any hint/documentation for better understanding is appreciated.

Expected result:

$newDataSet = array(
     '2017-01-02' => array(
      array(
        array(
         'name' => 'Paul',
         'state' => 'in',
         'date' => '2017-01-02'    
        ),
        array(
         'name' => 'Paul',
         'state' => 'out',
         'date' => '2017-01-02'    
        )
      )
     ),
    '2017-02-03' => array(
      array(
        array(
         'name' => 'John',
         'state' => 'in',
         'date' => '2017-02-03'    
        ),
        array(
         'name' => 'John',
         'state' => 'out',
         'date' => '2017-02-03'    
        )
      )
     ),
    '2017-04-01' => array(
      array(
        array(
         'name' => 'Smith',
         'state' => 'in',
         'date' => '2017-04-01'    
        ),
        array(
         'name' => 'Smith',
         'state' => 'out',
         'date' => '2017-04-01'    
        )
      )
     )
    );
8
  • Is php.net/manual/en/function.array-multisort.php what you are looking for? Commented Jun 20, 2017 at 14:54
  • Hello @GisoStallenberg I'm looking to sort by date ASC as from the database I have a UNION ALL that give me first set and second and is pre-sorted by date asc but not at all, i do a foreach and group by date and i create a new set of array, but as you can see the date is not sorted by ASC. Thx Commented Jun 20, 2017 at 14:58
  • 1
    Ok, for me it is very unclear what the expected result would be. Can you add that expected result? Commented Jun 20, 2017 at 15:04
  • The inner array have same date, no need to sort Commented Jun 20, 2017 at 15:04
  • I guess you are not sort by key date, but by the index of `$newDataSet' Commented Jun 20, 2017 at 15:05

1 Answer 1

3

you can use ksort(), live demo.

ksort($newDataSet);
print_r($newDataSet);
Sign up to request clarification or add additional context in comments.

1 Comment

i already have tried this way but was'n working, i cannot understand why, maybe i has something wrong in my query but now is ok, is sorted correctly. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.