I'm 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.
What I have so far is this:
array_multisort(
$sort1,
SORT_ASC,
$sort2,
SORT_ASC,
$sort3,
SORT_ASC,
$arraytosort
);
I would like to write something that will allow a more flexible/dynamic payload of sorting data/rules. Something like this:
$dynamicSort = "$sort1,SORT_ASC,$sort2,SORT_ASC,$sort3,SORT_ASC,";
array_multisort(
$dynamicSort,
$arraytosort
);
How can I feed an unknown number of parameters to array_multisort()
and have it modify the $arraytosort
array?
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)