note 99700 added to function.sort
| From: | MatthewFedak at osu1 dot php dot net | Date: | Tue, 31 Aug 2010 14:44:09 +0000 |
| Subject: | note 99700 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172197@lists.php.net to get a copy of this message | ||
The code below will sort via a field passed as the $on parameter, 3rd parameter is
'SORT_DESC' or 'SORT_ASC'. The function from the previous developer below mine
maintains the array index value good for associative arrays but for normal arrays with associative
fields like database queries returned as arrays use this.
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 217.40.248.178
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/99700
Del: integrated -- https://master.php.net/note/delete/99700/integrated
Del: useless -- https://master.php.net/note/delete/99700/useless
Del: bad code -- https://master.php.net/note/delete/99700/bad+code
Del: spam -- https://master.php.net/note/delete/99700/spam
Del: non-english -- https://master.php.net/note/delete/99700/non-english
Del: in docs -- https://master.php.net/note/delete/99700/in+docs
Del: other reasons-- https://master.php.net/note/delete/99700
Reject -- https://master.php.net/note/reject/99700
Search -- https://master.php.net/manage/user-notes.php