php - Sort Multi-dimension array by date with usort -


i'm trying sort multi-dimensional array date using usort() can't seem work.

start array:

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

and assigned 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 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 sort it:

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

and function itself:

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

and output data

foreach($dayevents $outputdata) {     $calendar .= $outputdata['data']; } 

but isn't sorting date, , error notice: undefined index: data

can see i'm going wrong? i've never used usort before , instructions i've tried follow questions on here don't seem work.

i believe problem in foreach statement. instead of this:

foreach($dayevents $outputdata) { 

you need this:

foreach($dayevents['output'] $outputdata) { 

for first statement, data undefined, since have single element in array called 'output'. in second, 'data' should defined.

you need add uasort call:

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

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -