0

I have a multidimensional array called $arrActivities.

In order to use php's array_multisort, I have created four arrays: $arrField0,$arrField1, $arrField2 and $arrField3, which are all arrays of specific fields found in $arrActivities.

Using those arrays, this command works perfectly:

array_multisort($arrField0, SORT_STRING, $arrField1, SORT_STRING, $arrField2, SORT_STRING, $arrField3, SORT_STRING, $arrActivities);

I need to create that parameter string dynamically, though, as sometimes there may be five arrays depending on the dataset.

I tried dynamically creating a string:

$strSort = '$arrField0, SORT_STRING, $arrField1, SORT_STRING, $arrField2, SORT_STRING, $arrField3, SORT_STRING, $arrActivities';

This works (ie it does the sorting correctly) but I get a warning:

array_multisort($strSort);

Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag

What is the right way to pass in the arguments with a string or array where I don't get a warning?

Why do I get the warning but it sorts correctly?

sws
  • 1
  • 2
  • We may be able to help you with an elegant solution if you provide some sample input that adds context to your question. Show us some input and output data please. – mickmackusa May 17 '17 at 23:18
  • I added the code to my entry above. – sws May 19 '17 at 16:57
  • I haven't had time to have a good look at your question update, but I'll come back when I can. Please provide some simple sample array data so that I can visualize what you are working with and what you are expecting as a result. – mickmackusa May 19 '17 at 21:16
  • ...what I mean is, show me the actual array build with sample values and your sorting intentions. I am sure that there is a better means of organizing your data. – mickmackusa May 20 '17 at 05:29
  • I posted the solution. Thanks for your help. – sws May 23 '17 at 21:52
  • Please post your solution as an answer and then accept it. This will tell the system and volunteers that your question is resolved. – mickmackusa May 23 '17 at 22:25

1 Answers1

0

SOLUTION:

I found the solution of building a dynamic list of parameters for array_multisort by using call_user_func_array:

    $arrSort = array(&$arrField0, SORT_STRING, &$arrField1, SORT_STRING, &$arrField2, SORT_STRING, &$arrField3, SORT_STRING); 
    $arrParams = array_merge($arrSort, array(&$arrActivities));
    call_user_func_array('array_multisort', $arrParams);

I am able to produce the $arrSort array dynamically and it sorts correctly without an error or warning.

sws
  • 1
  • 2