note 40626 added to function.sort
| From: | GerhardConradi at rn2 dot php dot net | Date: | Wed, 10 Mar 2004 05:49:37 +0000 |
| Subject: | note 40626 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66524@lists.php.net to get a copy of this message | ||
Hi @all,
to my previous author: thanks, thanks, thanks! Works great and saved me lot of time. I added an
extension for ascending/descending order. Here are the changes:
// first i defined two constants for asc and desc
// you can skip that, but then you have to change the switch command at the end of the function
define("ORDER_ASC", "asc");
define("ORDER_DESC", "desc");
function mu_sort($array, $key_sort, $asc_desc) { // start function
// check input parameters
if (func_num_args()<2 || func_num_args()>3) die("Wrong number of parameters for the call
of mu_sort()");
$array = func_get_arg(0);
$key_sort = func_get_arg(1);
if (func_num_args()==3)
$asc_desc = func_get_arg(2);
else
$asc_desc = ORDER_ASC;
$key_sorta = explode(",", $key_sort);
.... // from here it's exactly the same
... // until here
} // end loop #2
} // end loop #1
// sort
// if you don't want to use the constants defined at the top, change here to whatever
you're using
switch($asc_desc) {
case ORDER_DESC:
rsort($output); break;
default:
sort($output);
}
// return sorted array
return $output;
} // end function
Now you can call the function like this:
mu_sort($array, $key_sort, ORDER_DESC);
or even:
mu_sort($array, $key_sort); // This will sort the array by default ascending
Have fun...
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40626
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40626&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40626&report=yes
Search -- http://master.php.net/manage/user-notes.php