<?php
$array = array(
0 =>array(
'name' => 'Rnukir',
'total' => '9m'),
1 => array(
'name' => 'Arnesista',
'total' => '1m 45s'),
2 => array(
'name' => 'Omas',
'total' => '1m 42s'),
3 => array(
'name' => 'John',
'total' => '11m')
);
//1 - String value convert to time in second
$modified_array = array();
foreach($array as $key=>$each) {
$each["total"] = stringToTime($each["total"]);
$modified_array[$key] = $each;
}
//2 - Sort array based on second
usort($modified_array, "cmp");
//3 - Convert second to string
$final_array = array();
foreach($modified_array as $key=>$each) {
$each["total"] = timeToString($each["total"]);
$final_array[$key] = $each;
}
//4 - See output
print_r($final_array);
function stringToTime($str)
{
$str_array = explode(" ",$str);
$v = 0;
foreach($str_array as $key=>$value) {
$v += substr($value,-1)=='m' ? substr($value,0,-1)*60 : substr($value,0,-1)*1;
}
return $v;
}
function timeToString($time)
{
$m = intval($time/60);
$s = $time%60;
if(empty($s))
return $m."m";
else
return $m."m ".$s."s";
}
function cmp($a, $b)
{
if ($a['total'] == $b['total']) {
return 0;
}
return ($a['total'] > $b['total']) ? -1 : 1;
}
?>