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?

6
  • 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. Commented May 17, 2017 at 23:18
  • I added the code to my entry above. Commented May 19, 2017 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. Commented May 19, 2017 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. Commented May 20, 2017 at 5:29
  • I posted the solution. Thanks for your help. Commented May 23, 2017 at 21:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.