I have an array which generated using PHP as below:
Array (
[0] => Array ( [user] => test 1 [esttime] => 5 mins [destination] => testing location svvfefhsrdfd )
[1] => Array ( [user] => test 2 [esttime] => 5 mins [destination] => testing location fsdfdsv )
[2] => Array ( [user] => test 5 [esttime] => 8 mins [destination] => testing location scvvfe )
[3] => Array ( [user] => test 3 [esttime] => 5 mins [destination] => testing location sfds)
[4] => Array ( [user] => test 4 [esttime] => 8 mins [destination] => testing location gfsdarr )
[5] => Array ( [user] => test 6 [esttime] => 10 mins [destination] => testing location dgfd )
)
the array have keys user,estimate time and destination and related values, i need to sort this array using esttime key value.
i tried many ways, but couldn't able to find a way.
anyone know how to sort this array using php, thank you
You could use custom sorting usort()
in this case, then just use strtotime()
for that relative time:
usort($array, function($a, $b){
$time_a = strtotime($a['esttime']);
$time_b = strtotime($b['esttime']);
return $time_a - $time_b;
});
echo '<pre>';
print_r($array);
One way to do it is to generate a new array:
foreach($array as $row) {
$new[str_replace(" ","_",$row['esttime'])][] = $row;
}
print_r($new) Should look like this after:
Array (
[5_mins][0] => Array ([user] => test 1 [esttime] => 5 mins [destination] => testing location svvfefhsrdfd)
[5_mins][1] => Array ( [user] => test 2 [esttime] => 5 mins [destination] => testing location fsdfdsv)
[5_mins][2] => Array ( [user] => test 3 [esttime] => 5 mins [destination] => testing location sfds)
[8_mins][0] => Array ( [user] => test 5 [esttime] => 8 mins [destination] => testing location scvvfe )
[8_mins][1] => Array ( [user] => test 4 [esttime] => 8 mins [destination] => testing location gfsdarr )
[10_mins][0] => Array ( [user] => test 6 [esttime] => 10 mins [destination] => testing location dgfd )
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.