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.

3
  • You need to use strtotime() to convert to a timestamp, then you can easily use one of the sort() family of functions. Commented Nov 17, 2011 at 12:36
  • There are no times in that array. Commented Nov 17, 2011 at 12:38
  • 1
    possible duplicate of How to Compare Dates in php? Commented Nov 17, 2011 at 12:39

2 Answers 2

2

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

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

Comments

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]);

Comments

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.