Sorry if this is duplicate, I have tried to look at previous posts, but maybe my limited understanding of php gets in the way.
This works to sort arrays 1 and 3 by array 2:
<?php
$ar[1] = array("game","scissors","rock","paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] =array("four","two","one","three");
array_multisort($ar[2],$ar[1],$ar[3]);
for ($j=0;$j<=3;$j++) {
for ($i=1;$i<=3;$i++) {
echo $ar[$i][$j]." ";
}
echo "\n";
}
?>
output:
rock A one
scissors B two
paper C three
game D four
Fine. But I want the list of arrays used as argument for array_multisort()
to be specified by user input, so it can't be literal. Instead, I would like the program to decide the order of arrays in the list based on the user input and then name the resulting array of arrays $supar (superarray). But when using $supar as argument for array_multisort()
, no sorting is carried out.
<?php
$ar[1] = array("game","scissors","rock","paper");
$ar[2] = array("D", "B", "A", "C");
$ar[3] =array("four","two","one","three");
$supar = array($ar[2],$ar[1],$ar[3]);
array_multisort($supar);
for ($j=0;$j<=3;$j++) {
for ($i=1;$i<=3;$i++) {
echo $ar[$i][$j]." ";
}
echo "\n";
}
?>
Output:
game D four
scissors B two
rock A one
paper C three
How should I do to make array_multisort()
sort the arrays in $supar by a specified $ar array?
Thank you for your patience.