tags:

views:

62

answers:

1

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?

+2  A: 

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)
St. John Johnson
I had the same thought and went to try, and it does seem to work on built-in functions (despite the name!)
Chris Smith
hmmm seems like a perfect solutions but I keep getting "Argument #1 is expected to be an array or a sort flag" even though it works normally.
websiteguru
Oh, try single quotes instead of double quotes. It may be trying to pass the value of $sort1 instead the name "sort1"
St. John Johnson