PHP: Building an array of dates with two types of sorting -
i have array of appointments need order in special way. let's current date 30 may 2016 , current time 11:00h:
$currenttimestamp = strtotime("05/30/2016 11:00"); // results in 1464620400
first, here comes array of appointments contains timestamps end of each appointment:
$alldates = [ '2016-05-24' => [ [ 'end' => 1464086700 ] ], '2016-05-27' => [ [ 'end' => 1464349500 ] ], '2016-05-30' => [ [ 'end' => 1464606900 ], [ 'end' => 1464614100 ], [ 'end' => 1464623100 ] ], '2016-05-31' => [ [ 'ende' => 1464705900 ] ]
];
here come requirements resorting array:
- all appointments end later current timestamp must ordered descending.
- all appointments end earlier current timestamp must ordered ascending.
my desired output following:
$desiredfinalarrdates = [ '2016-05-31' => [ [ 'end' => 1464705900 ] ], '2016-05-30' => [ [ 'end' => 1464623100 ] ], // !!! past dates start here, ascending... '2016-05-24' => [ [ 'end' => 1464086700 ] ], '2016-05-27' => [ [ 'end' => 1464349500 ] ], '2016-05-30' => [ [ 'end' => 1464606900 ], [ 'end' => 1464614100 ] ] ];
how achieve desired ordering of $alldates array?
-------------- attempt far ----------------
$future = []; $past = []; foreach ($alldates $key => $subarr) { foreach ($subarr $value) { if ($value['end'] >= $currenttimestamp) { $future[$key][] = $value; } else { $past[$key][] = $value; } } } asort($past); $desiredfinalarrdates = array_merge($future, $past);
the problem array_merge keys overwritten.
if performance doesn't matter, can this.
1. create 2 arrays $arrbefore , $arrafter. 2. iterate $alldates array (and sub-arrays) , compare current value $currenttimestamp. 3. if lower add $arrbefore array, else $arrafter. 4. sort arrays correspondigly (descending/ascending). 5. finally, merge 2 arrays.
edit: create 2 arrays $arrbefore , $arrafter
sorted correctly. achieve desired output, not possible in form. that's because can't have same key multiple times in array (i.e. 2016-05-30
in case).
<?php $currenttimestamp = strtotime("05/30/2016 11:00"); $alldates = [ '2016-05-24' => [ [ 'end' => 1464086700 ] ], '2016-05-27' => [ [ 'end' => 1464349500 ] ], '2016-05-30' => [ [ 'end' => 1464606900 ], [ 'end' => 1464614100 ], [ 'end' => 1464623100 ] ], '2016-05-31' => [ [ 'end' => 1464705900 ] ] ]; $arrbefore = array(); $arrafter = array(); foreach($alldates $currkey => $currdatearr){ foreach($currdatearr $currdate){ if($currdate['end'] < $currenttimestamp){ if(array_key_exists($currkey,$arrbefore)){ $arrbefore[$currkey][] = $currdate['end']; }else{ $arrbefore[$currkey] = array($currdate['end']); } }else{ if(array_key_exists($currkey,$arrafter)){ $arrafter[$currkey][] = $currdate['end']; }else{ $arrafter[$currkey] = array($currdate['end']); } } } } //sort arrays ksort($arrbefore); krsort($arrafter); //sort sub-arrays foreach($arrbefore $subarr){ asort($subarr); } //sort sub-arrays foreach($arrafter $subarr){ rsort($subarr); } print_r($arrbefore); print_r($arrafter);
prints:
array ( [2016-05-24] => array ( [0] => 1464086700 ) [2016-05-27] => array ( [0] => 1464349500 ) [2016-05-30] => array ( [0] => 1464606900 [1] => 1464614100 ) ) array ( [2016-05-31] => array ( [0] => 1464705900 ) [2016-05-30] => array ( [0] => 1464623100 ) ) ?>