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'    
        )
      )
     )
    );
LF00
  • 27,015
  • 29
  • 156
  • 295
ndAR
  • 371
  • 2
  • 4
  • 15
  • Is http://php.net/manual/en/function.array-multisort.php what you are looking for? – Giso Stallenberg Jun 20 '17 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 – ndAR Jun 20 '17 at 14:58
  • 1
    Ok, for me it is very unclear what the expected result would be. Can you add that expected result? – Giso Stallenberg Jun 20 '17 at 15:04
  • The inner array have same date, no need to sort – LF00 Jun 20 '17 at 15:04
  • I guess you are not sort by key `date`, but by the index of `$newDataSet' – LF00 Jun 20 '17 at 15:05
  • @KrisRoofe exactly, what i need to sort is the key of '$newDataSet' but i can't find the right function. – ndAR Jun 20 '17 at 15:07
  • a simple `ksort` sorts by key – apokryfos Jun 20 '17 at 15:08
  • Possible duplicate of [php - Sort array with date as key](https://stackoverflow.com/questions/7134776/php-sort-array-with-date-as-key) – mickmackusa Jun 20 '17 at 15:44

1 Answers1

3

you can use ksort(), live demo.

ksort($newDataSet);
print_r($newDataSet);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 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 – ndAR Jun 20 '17 at 15:36