1

Here is the content of my array:

array(
  'June 01, 2014' => array(
    (int) 722107 => array(
        'date' => 'June 01, 2014',
        'start_time' => '2:00 AM',
        'end_time' => '03:00 AM'
    ),
    (int) 117646 => array(
        'date' => 'June 01, 2014',
        'start_time' => '12:30 AM',
        'end_time' => '03:30 AM'
    )
  ),
  'May 31, 2014' => array(
    (int) 769349 => array(
        'date' => 'May 31, 2014',
        'start_time' => '12:30 AM',
        'end_time' => '03:30 AM'
    )
  )
)

And the output I want is it should sorted first by keys (Date in ASC), then sort again the values of each keys by the start time in ASC.

Based on the example above it should output:

array(
     'May 31, 2014' => array(
    (int) 769349 => array(
        'date' => 'May 31, 2014',
        'start_time' => '12:30 AM',
        'end_time' => '03:30 AM'
    )
 ),
'June 01, 2014' => array(

    (int) 117646 => array(
        'date' => 'June 01, 2014',
        'start_time' => '12:30 AM',
        'end_time' => '03:30 AM'
    ),
    (int) 722107 => array(
        'date' => 'June 01, 2014',
        'start_time' => '2:00 AM',
        'end_time' => '03:00 AM'
    )
   )

    )
5
  • So you want to sort it by the date? Commented Jun 26, 2014 at 3:01
  • @Darren Yes the date the first keys, then once it is sorted by keys. Sort again the values of each keys base on start_time so everything will be in order. Commented Jun 26, 2014 at 3:03
  • Is this coming from a database? If so, easy enough to sort it in your find query Commented Jun 26, 2014 at 3:09
  • @scrowler, its not coming from a database, its easier that way, but its from a session variable. Commented Jun 26, 2014 at 3:09
  • Is this the result of some sort of SQL query? I understand that you are looking for a PHP solution, but maybe you overlooked something, like sorting in the SQL query itself. Again: I don't know if it is of any concern here, but I'm just thinking with you ;-) Commented Jun 26, 2014 at 8:16

2 Answers 2

5

The two passes can be with uksort() and uasort() using a custom function to compare two dates:

function timeCompare($a, $b)
{
    $va = strtotime($a);
    $vb = strtotime($b);

    if ($va != $vb) {
        return $va < $vb ? -1 : 1;
    }
    return 0;
}

// first pass (sort outer elements)
uksort($data, 'timeCompare');
// second pass (sort inner elements)
foreach ($data as &$item) {
    uasort($item, function($a, $b) {
        return timeCompare($a['start_time'], $b['start_time']);
    });
}

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

@zynder I've tested it with your given input and it produces the correct output with that; please give counter example (also, check the added demo).
Yes, I'm sorry about that Jack. Thank you to the both of you Jack and @darren.
2

this will sort it the way you requested.

$sort = array();
foreach ($d as $key => $item) {
    $sort[$key] = strtotime($key);
}
array_multisort($sort, SORT_ASC, $d);

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

foreach ($d as $k => $i) {
    usort($i, "sortme");
}

Which returns:

Array
(
    [May 31, 2014] => Array
        (
            [769349] => Array
                (
                    [date] => May 31, 2014
                    [start_time] => 12:30 AM
                    [end_time] => 03:30 AM
                )

        )

    [June 01, 2014] => Array
        (
            [722107] => Array
                (
                    [date] => June 01, 2014
                    [start_time] => 2:00 AM
                    [end_time] => 03:00 AM
                )

            [117646] => Array
                (
                    [date] => June 01, 2014
                    [start_time] => 12:30 AM
                    [end_time] => 03:30 AM
                )

        )

)

Demo


Explanation

  • Basically the first function will sort the dates May 31, 2014/ June 01, 2014/ ETC.
  • The second step is the foreach loop with goes through the child arrays to sort them my the date/time.
  • Voila, sorted array :-)

2 Comments

@zynder Sorry yes $d is the data parsed through (The original array)
Also on that part on the key June 1, 2014, the child array first index must be the 117646 because its start time is 12:30am.

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.