PHP sort based on offline or online
I've got a multidimensional array such as the following:
[Jack] => Array
(
[GCF] => Array
(
[Name] => Jack
[retrieved] => cache
)
[PF] => Array
(
[lastSeen] => Fri, 03 Dec 2010 23:48:14 GMT
[online] => 0
)
)
... More users here in the same format
From this I want to sort the array so online users are displayed first, but I want PHP not to modify the array or any 开发者_开发技巧of its keys if possible.
Anyone have any ideas about how to accomplish this?
Thanks.
function array_sort($array, $on, $order='SORT_DESC')
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch($order)
{
case 'SORT_ASC':
echo "ASC";
asort($sortable_array);
break;
case 'SORT_DESC':
echo "DESC";
arsort($sortable_array);
break;
}
foreach($sortable_array as $k => $v) {
$new_array[] = $array[$k];
}
}
return $new_array;
}
$users = <input array>
$online = $offline = array();
foreach ($users as $name => $data) {
if ($data['PF']['online']) {
$online[$name] = $data;
} else {
$offline[$name] = $data;
}
}
$sorted = array_merge($online, $offline);
精彩评论