<?php
bool array_multisort ( arr &$array1 [,
mix $array1_sort_order = SORT_ASC [,
mix $array1_sort_flags = SORT_REGULAR [,
mix $array2, ..., mix $arrayN ]]] )
where,
&$array1 = The input ARRAY to be sorted
$array1_sort_order = To control de sorting behavior
( SEE the below TABLE )
$array1_sort_flags = The sort options for the previous ARRAY
( SEE the below TABLE )
$array2 = The second ARRAY followed by sort order or flags
. . . . . . . . . . .
$arrayN = The last ARRAY followed by sort order or flags
?>
CONSTANT | VALUE | OBSERVATIONS | DEFAULT |
SORT_ASC | 4 | sort ascendingly | SORT_ASC |
SORT_DESC | 3 | sort descendingly | |
ed48 |
CONSTANT | VALUE | OBSERVATIONS | DEFAULT |
SORT_REGULAR | 0 | compare items normally (don't change types). |
SORT_REGULAR |
SORT_NUMERIC | 1 | compare items numerically | |
SORT_STRING | 2 | compare items as strings | |
SORT_LOCALE_STRING Does not work in PHP 8 |
5 | compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale. |
|
SORT_NATURAL | 6 | compare items as strings using "natural ordering" like natsort. | |
SORT_FLAG_CASE | 8 | can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively | |
ed48 |
<?php
$arrms001 = [ 37, 17, 27, 7 ];
echo 'The given ARRAY:<br>';
print_r($arrms001);
array_multisort($arrms001);
echo '<br><br>ARRAY - DEFAULT:<br>';
print_r($arrms001);
?>
<?php
$arrms002a = [ "TEN" => "ten", 8 => "eight", "5" => "five" ];
$arrms002b = [ 0, 3, "SEVEN" ];
echo 'The given ARRAY (1):<br>';
print_r($arrms002a);
echo '<br><br>The given ARRAY (2):<br>';
print_r($arrms002b);
echo '<br><br><br>';
if(array_multisort($arrms002a, $arrms002b))
{
echo 'ARRAY (1) - SORT_REGULAR:<br>';
print_r($arrms002a);
echo '<br><br>ARRAY (2) - SORT_REGULAR:<br>';
print_r($arrms002b);
}
else
{
echo 'It was not possible to sort any of the ARRAYS.';
}
?>
<?php
$arrms04a = [ "zero" => [ "ten" => 10, "eight" => 8 ], "five" => 5 ];
$arrms04b = [ "one" => [10 => "ten", 8 => "eight" ], 5 => "five" ];
echo 'ARRAY (1)<br>';
print_r($arrms04a);
echo '<br><br>';
echo 'ARRAY (2)<br>';
print_r($arrms04b);
echo '<br><br><br>';
array_multisort($arrms04a,
SORT_DESC, SORT_REGULAR, $arrms04b);
echo 'ARRAY (1) - SORT_DESC, SORT_REGULAR<br>';
print_r($arrms04a);
echo '<br><br>';
echo ' ARRAY (2) - SORT_DESC, SORT_REGULAR<br>';
print_r($arrms04b);
echo '<br><br><br>';
array_multisort($arrms04a,
SORT_ASC, SORT_REGULAR, $arrms04b);
echo 'ARRAY (1) - SORT_ASC, SORT_REGULAR<br>';
print_r($arrms04a);
echo '<br><br>';
echo 'ARRAY (2) - SORT_ASC, SORT_REGULAR<br>';
print_r($arrms04b);
?>
<?php
$arrms05a = [ [ "ten" => 10, "eight" => 8 ], "five" => 5 ];
$arrms05b = [ [10 => "ten", 8 => "eight" ], 5 => "five" ];
echo 'ARRAY (1)<br>';
print_r($arrms05a);
echo '<br><br>';
echo 'ARRAY (2)<br>';
print_r($arrms05b);
echo '<br><br><br>';
array_multisort($arrms05a,
SORT_ASC, SORT_REGULAR, $arrms05b);
echo 'ARRAY (1) - SORT_ASC, SORT_REGULAR<br>';
print_r($arrms05a);
echo '<br><br>';
echo 'ARRAY (2) - SORT_ASC, SORT_REGULAR<br>';
print_r($arrms05b);
echo '<br><br><br>';
array_multisort($arrms05a,
SORT_DESC, SORT_REGULAR, $arrms05b);
echo 'ARRAY (1) - SORT_DESC, SORT_REGULAR<br>';
print_r($arrms05a);
echo '<br><br>';
echo 'ARRAY (2) - SORT_DESC, SORT_REGULAR<br>';
print_r($arrms05b);
?>
<?php
$arrms06a = [ 2, 4, -5, -3, 1 ];
$arrms06b = [ 'two', 'four', 'minus five', 'minus three', 'one' ];
echo 'ARRAY (1)<br>';
print_r($arrms06a);
echo '<br><br>';
echo 'ARRAY (2)<br>';
print_r($arrms06b);
echo '<br><br><br>';
array_multisort($arrms06a,
SORT_ASC, SORT_STRING, $arrms06b);
echo 'ARRAY (1) - SORT_ASC, SORT_STRING<br>';
print_r($arrms06a);
echo '<br><br>';
echo 'ARRAY (2) - SORT_ASC, SORT_STRING<br>';
print_r($arrms06b);
array_multisort($arrms06a,
SORT_DESC, SORT_STRING, $arrms06b);
echo '<br><br>';
echo 'ARRAY (1) - SORT_DESC, SORT_STRING<br>';
print_r($arrms06a);
echo '<br><br>';
echo 'ARRAY (2) - SORT_DESC, SORT_STRING<br>';
print_r($arrms06b);
?>
<?php
echo "Basic functionality:<br><br>";
// Initialise all required variables
$ar1 = array("row1" => 2, "row2" => 1, "row3" => 1);
$ar2 = array("row1" => 2, "row2" => "aa", "row3" => "1");
echo "Testing with all normal arguments:<br>";
var_dump( array_multisort($ar1, SORT_ASC, SORT_REGULAR,
$ar2, SORT_DESC, SORT_STRING) );
echo '<br>';
var_dump($ar1, $ar2);
echo "<br><br>Testing with one argument:<br>";
var_dump( array_multisort($ar2) );
echo '<br>';
var_dump($ar2);
?>
<?php
echo "Basic functionality -
renumbering of numeric keys:<br><br>";
// Initialise all required variables
$ar1 = array( "strkey" => 2, 1, 9 => 1);
$ar2 = array( 2, "aa" , "1");
echo "Testing with all normal arguments:<br>";
var_dump( array_multisort($ar1, SORT_ASC, SORT_REGULAR,
$ar2, SORT_ASC, SORT_NUMERIC) );
echo '<br>';
var_dump($ar1, $ar2);
?>
<?php
echo "Case-sensitive:<br><br>";
$a = array(
'Second',
'First.1',
'First.2',
'First.3',
'Twentieth',
'Tenth',
'Third',
);
$b = array(
'2 a',
'1 bb 1',
'1 bB 2',
'1 BB 3',
'20 c',
'10 d',
'3 e',
);
array_multisort($b, SORT_STRING, $a);
echo '<pre>';
var_dump($a, $b);
echo '</pre>';
?>
<?php
echo "Case-insensitive:<br><br>";
$a = array(
'Second',
'First.1',
'First.2',
'First.3',
'Twentieth',
'Tenth',
'Third',
);
$b = array(
'2 a',
'1 bb 1',
'1 bB 2',
'1 BB 3',
'20 c',
'10 d',
'3 e',
);
array_multisort($b, SORT_STRING | SORT_FLAG_CASE, $a);
var_dump($a, $b);
?>
<?php
echo "Testing natural sorting<br><br>";
$a = array(
'Second',
'First',
'Twentieth',
'Tenth',
'Third',
);
$b = array(
'2 a',
'1 b',
'20 c',
'10 d',
'3 e',
);
array_multisort($b, SORT_NATURAL, $a);
var_dump($a, $b);
?>
<?php
echo "Testing natural sorting case-sensitive:<br><br>";
$a = array(
'Second',
'First.1',
'First.2',
'First.3',
'Twentieth',
'Tenth',
'Third',
);
$b = array(
'2 a',
'1 bb 1',
'1 bB 2',
'1 BB 3',
'20 c',
'10 d',
'3 e',
);
array_multisort($b, SORT_NATURAL, $a);
var_dump($a, $b);
?>
<?php
// Something of a dummy example
// where 0 and '0' are used as equal elements.
$ary1 = array_fill(0, 100, 0);
$origAry2 = $ary2 = array_merge(...array_fill(0, 50, [0, '0']));
array_multisort($ary1, $ary2);
var_dump($ary2 === $origAry2);
?>
<?php
echo "Testing with anonymous arguments:<br><br>";
var_dump(array_multisort(array(1,3,2,4)));
?>
<?php
echo "Testing with empty array:<br><br>";
var_dump(array_multisort(array()));
?>
<?php
echo "Testing with multiple array arguments:<br><br>";
$arr1 = array (4,3,3,3);
$arr2 = array (9,3,2,2);
$arr3 = array (9,9,2,1);
var_dump(array_multisort($arr1, $arr2, $arr3));
echo '<pre>';
var_dump($arr1);
echo '<br><br>';
var_dump($arr2);
echo '<br><br>';
var_dump($arr3);
echo '</pre>';
?>
<?php
echo "Testing all array sort specifiers:<br><br>";
$ar = array( 2, "aa" , "1");
array_multisort($ar, SORT_REGULAR, SORT_ASC);
var_dump($ar);
echo '<br><br>';
array_multisort($ar, SORT_STRING, SORT_ASC);
var_dump($ar);
echo '<br><br>';
array_multisort($ar, SORT_NUMERIC, SORT_ASC);
var_dump($ar);
?>
<?php
echo "Testing all array sort specifiers:<br><br>";
$ar = array( 2, "aa" , "1");
array_multisort($ar, SORT_REGULAR, SORT_DESC);
var_dump($ar);
echo '<br><br>';
array_multisort($ar, SORT_STRING, SORT_DESC);
var_dump($ar);
echo '<br><br>';
array_multisort($ar, SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
<?php
setlocale(LC_ALL, 'pt-PT', 'pt_PT');
$arn = [ 21.98, 3.8, 0.8, 1, 0 , 6 ];
array_multisort($arn, SORT_LOCALE_STRING);
echo '<pre>';
var_dump($arn);
echo '</pre>';
?>