Im trying to sort any array with array_multisort and everything is working great. However, based on conditions in my script, I need to change the options. So What I have so far is this:
array_multisort(
$sort1,SORT_ASC,
$sort2,SORT_ASC,
$sort3,SORT_ASC,
$arraytosort
);
and what I would like to have is something like this:
$dynamicSort = "$sort1,SORT_ASC,$sort2,SORT_ASC,$sort3,SORT_ASC,";
array_multisort(
$dynamicSort,
$arraytosort
);
Any suggestions?
You could try to use call_user_func_array. But I've never tried it on a built-in function before. Here is an example:
$dynamicSort = "$sort1,SORT_ASC,$sort2,SORT_ASC,$sort3,SORT_ASC";
$param = array_merge(explode(",", $dynamicSort), array($arrayToSort))
call_user_func_array('array_multisort', $param)
I had the same problem with this answer: "Argument #1 is expected to be an array or a sort flag"
For anyone having the same problem try this instead:
$dynamicSort = array(&$sort1, SORT_ASC, &$sort2, SORT_ASC, &$sort3, SORT_ASC);
$param = array_merge($dynamicSort, array(&$arrayToSort));
call_user_func_array('array_multisort', $param);
Note that i have used the reference to my variables "&$" instead of $. This works great in php 5.3 but may cause error in 5.2 due to a bug.
It is important to understand that the array sent to call_user_func_array() must consist only of references; it is not important whether the array itself is passed by reference. I spent the better part of a day troubleshooting this; the fact that the examples on the function page at php.net all used literal arrays led me to this page: php Bug #49353. Problem solved.
This doesn't seem to be very well (or consistently) documented, so here goes....
These DO NOT WORK (PHP 5.3.3):
$multisort_array = array($arr1, SORT_DESC, SORT_STRING, $arr2); // array of values
call_user_func_array('array_multisort', $multisort_array); // array passed by value
$multisort_array = array($arr1, SORT_DESC, SORT_STRING, $arr2); // array of values
call_user_func_array('array_multisort', &$multisort_array); // array passed by reference
$multisort_array = array(&$arr1, SORT_DESC, SORT_STRING, &$arr2); // non-constants by reference
call_user_func_array('array_multisort', $multisort_array); // array passed by value
$multisort_array = array(&$arr1, SORT_DESC, SORT_STRING, &$arr2); // non-constants by reference
call_user_func_array('array_multisort', &$multisort_array); // array passed by reference
These DO WORK:
$sort = array('desc' => SORT_DESC, 'string' => SORT_STRING);
$multisort_array = array(&$arr1, &$sort['desc'], &$sort['string'], &$arr2); // all by reference
call_user_func_array('array_multisort', $multisort_array); // array passed by value
$sort = array('desc' => SORT_DESC, 'string' => SORT_STRING);
$multisort_array = array(&$arr1, &$sort['desc'], &$sort['string'], &$arr2); // all by reference
call_user_func_array('array_multisort', &$multisort_array); // array passed by reference
To add onto the existing answers, just thought I would add a little something. For anyone passing the desired "sort by" as a comma-separated $_POST variable (or any comma-separated variable for that matter):
//$_POST["sort_by"] = "column_A DESC, column_B ASC, columns_C DESC";
$sort_bys = explode(",", $_POST["sort_by"]);
$dynamicSort = array();
foreach($sort_bys as $sort_by){
$sort_by2 = trim(str_replace('DESC','',$sort_by));
$direction = (strpos($sort_by, 'DESC') !== false)?SORT_DESC:SORT_ASC;
$$sort_by2 = array_column($array_to_sort, $sort_by2);
$dynamicSort[] = &$$sort_by2;
$dynamicSort[] = $direction;
$dynamicSort[] = SORT_NUMERIC; //or SORT_STRING or SORT_REGULAR ...
}
$param = array_merge($dynamicSort, array(&$array_to_sort));
call_user_func_array('array_multisort', $param);