Question

I'm trying to sort a multi-dimensional array by date using usort() but I can't seem to get it to work.

Start array:

$dayEvents = array();
$dayEvents['output'] = array();

And it is assigned like so:

$dayEvents['output'][] = array('date' => $dateStamp, 'data' => $dataOutput, 'ad' => $allDay);

Example output:

array(1) {
["output"]=>
  array(6) {
    [0]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 00:00:00"
      ["data"]=>
      string(115) "
2 tests were booked
"
      ["ad"]=>
      int(1)
    }
    [1]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:30:00"
      ["data"]=>
      string(316) "
08:30am
Matamata Class 1 R & F
"
      ["ad"]=>
      int(0)
}
    [2]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(319) "
08:00am-04:00pm
Truck Course
"
      ["ad"]=>
      int(0)
    }
    [3]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(328) "
08:00am-03:30pm
Trade Ed Rot Class 2
"
      ["ad"]=>
      int(0)
    }
    [4]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(326) "
08:00am-03:30pm
Trade Ed Tga Class 2
"
      ["ad"]=>
      int(0)
    }
    [5]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 17:00:00"
      ["data"]=>
      string(330) "
05:00pm-08:00pm
Tauranga Truck Course
"
      ["ad"]=>
      int(0)
    }
}
}

The statement to sort it:

if(count($dayEvents['output'])>1) {
    uasort($dayEvents, 'date_compare');
}

And the function itself:

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

And finally to output the data

foreach($dayEvents as $outputData) {
    $calendar .= $outputData['data'];
}

But it just isn't sorting by date, and I get the error Notice: Undefined index: data

Can anyone see where I'm going wrong? I've never used usort before and all the instructions I've tried to follow from questions on here don't seem to work.

Was it helpful?

Solution

I believe the problem is in your foreach statement. Instead of this:

foreach($dayEvents as $outputData) {

You need this:

foreach($dayEvents['output'] as $outputData) {

For the first statement, data is undefined, since you have a single element in that array called 'output'. In the second, 'data' should be defined.

You probably also need to add that into your uasort call:

uasort($dayEvents['output'], 'date_compare');

OTHER TIPS

The array-sorting functions in PHP only work with 1-dimensional arrays. To sort 2D-arrays you can use the following function of mine that I always use for these kind of tasks:

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
    $reference_array = array();

    foreach($array as $key => $row) {
        $reference_array[$key] = $row[$column];
    }

    array_multisort($reference_array, $direction, $array);
}

Adjusted your code:

array_sort_by_column($dayEvents['output'], 'date');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top