note 69241 deleted from function.array-multisort by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 10:28:10 +0000 |
| Subject: | note 69241 deleted from function.array-multisort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213681@lists.php.net to get a copy of this message | ||
Note Submitter: LPChip
----
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);
?>