note 69241 added to function.array-multisort
| From: | LPChip at osu1 dot php dot net | Date: | Mon, 28 Aug 2006 13:04:43 +0000 |
| Subject: | note 69241 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116431@lists.php.net to get a copy of this message | ||
I was looking for a way to dynamically multisort my array.
By dynamically I mean that its not static what column will be sorted and if its ASC or DESC, and the
ability to have more than one sorts.
This is the way a database would allow you to do.
The best way to dynamically do this, is by using eval.
The code below is partly what I used. (eg, I left out where the arrays were made and stuff, but the
important part is here.)
<?
$orderby_arr = array("col1 ASC";"col2 DESC");
// prepare multisort using eval
$eval_sort = "array_multisort(";
if ($orderby !="") {
$orderby_arr_c = count($orderby_arr);
for ($orderby_walk=0; $orderby_walk < $orderby_arr_c; $orderby_walk++) {
$pos = strpos($orderby_arr[$orderby_walk], " ");
$orderby_col = substr($orderby_arr[$orderby_walk], 0, $pos);
$orderby_type = substr($orderby_arr[$orderby_walk], $pos+1);
$eval_sort .= "\$this->OrderBy[$orderby_col]" . ", SORT_$orderby_type,";
}
}
$eval_sort .= " \$this->Current_Query);";
// if there's an array, sort it.
if ($this->Current_Query_m != -1) eval($eval_sort);
?>
----
Server IP: 83.137.20.120
Probable Submitter: 193.172.8.207 (proxied: 1.0 STAAA02)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/69241
Del: integrated -- https://master.php.net/note/delete/69241/integrated
Del: useless -- https://master.php.net/note/delete/69241/useless
Del: bad code -- https://master.php.net/note/delete/69241/bad+code
Del: spam -- https://master.php.net/note/delete/69241/spam
Del: non-english -- https://master.php.net/note/delete/69241/non-english
Del: in docs -- https://master.php.net/note/delete/69241/in+docs
Del: other reasons-- https://master.php.net/note/delete/69241
Reject -- https://master.php.net/note/reject/69241
Search -- https://master.php.net/manage/user-notes.php