2

Here is an extract of my test code and data:-

//Update Array 
$civs[1][1]="1";
$civs[1][2]="Inca";
$civs[2][1]="2";
$civs[2][2]="India";

//Sort Array 
array_multisort($civs[0][2], SORT_DESC, SORT_STRING,
            $civs[0][1], SORT_NUMERIC, SORT_DESC);

Output:-

Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in ~/test.php on line 3

array(3) { [1]=> array(2) { [1]=> string(1) "1" [2]=> string(4) "Inca" } [2]=> array(2) { [1]=> string(1) "2" [2]=> string(5) "India" } [0]=> array(2) { [2]=> NULL [1]=> NULL } }

Problem statement Depending on what the User selects I need to sort the array in PHP either on the 1st (numeric) or 2nd (alphanumeric) element of the 2nd part of the array but can't see how to do that, the various PHP sorts don't seem to allow you to tell it what is to be sorted on, have tried the above code . The PHP Manual for uksort says "If the array you wish to sort needs to be sorted by some non-trivial criteria," well numeric is about as trivial as it gets!

I've been looking at this for a couple of hours now and am sure I must be missing something as all I can see is that I have to write a custom function but surely PHP knows how to do simple sorts based on numeric or text values?

Main question I found was this:- How can I sort arrays and data in PHP? and I also spent a fair bit of time browsing through http://php.net/manual/en/array.sorting.php.

Apologies if I have missed something obvious.

Community
  • 1
  • 1
Alan Dev
  • 43
  • 5

0 Answers0