1
$arr1 = array("time"=>'2011 10-06 10:20:10', "val"=>20);
$arr2 = array("time"=>'2011 11-06 10:20:10', "val"=>20);
$arr3 = array("time"=>'2011 05-06 10:20:10', "val"=>20);
$arr4 = array("time"=>'2011 07-06 10:20:10', "val"=>20);
$arr5 = array("time"=>'2011 09-06 10:20:10', "val"=>20);


$arrGroup[1] = array($arr1, $arr2, $arr3, $arr4, $arr5); //Add key here


foreach ($arrGroup as $key => $row) {

    foreach($row as $rKey=> $rVal){

        $time[$rKey]  = $rVal['time'];

        $val[$rKey] = $rVal['val'];
    }

}

I would like to sort this array by its 'time'. However, the above code doesn't sort the array.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Acubi
  • 2,793
  • 11
  • 41
  • 54

2 Answers2

2

Convert the time to timestamp value and then use sort method to sort by date.

evilone
  • 22,410
  • 7
  • 80
  • 107
0

It would probably be simpler to use the timestamp as the index to the outer array if you want to maintain the data structure for your inner array

$arrGroup[1] = array(
    '1234567890' => $arr1, 
    '1234567891' => $arr2, 
    '1234567892' => $arr3,
    '1234567893' => $arr4, 
    '1234567894' => $arr5
); 

asort($arrGroup[1]);
Nick
  • 589
  • 1
  • 7
  • 28