array_multisort


php128 apg

SORT multiple or multi-dimensional ARRAY.





This function returns TRUE on success or FALSE on failure.

$array1_sort_flags can be swapped with $array1_sort_order or omitted entirely, in which case SORT_REGULAR is assumed.

Accepeted: $array2, $array3, ..., $arrayN optionally followed by sort order and flags.

Only elements corresponding to equivalent elements in previous arrays are compared.

In other words, the sort is lexicographical.



<?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

?>
 

&$array


The input ARRAY. to be sorted



 $array1_sort_order 


It can be one of the following values:

CONSTANT VALUE OBSERVATIONS DEFAULT
SORT_ASC 4 sort ascendingly SORT_ASC
SORT_DESC 3 sort descendingly
ed48


 $array1_sort_flags 


It can be one of the following values:

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


  1 EXERCISE   

<?php

$arrms001 
= [ 371727];

echo 
'The given ARRAY:<br>';
print_r($arrms001);

array_multisort($arrms001);

echo 
'<br><br>ARRAY - DEFAULT:<br>';
print_r($arrms001);

?> 

 RESULT   

The given ARRAY:
array
{
[0] => 37
[1] => 17
[2] => 27
[3] => 7
}

ARRAY - DEFAULT:
array
{
[0] => 7
[1] => 17
[2] => 27
[3] => 37
}


This is a one-dimensional ARRAY.

In the ordering there was a reordering of the keys.


  2 EXERCISE   

<?php

$arrms002a 
= [ "TEN" => "ten"=> "eight""5" => "five" ];
$arrms002b = [ 03"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.';
}

?> 

 RESULT   

The given ARRAY (1)
Array ( [TEN] => ten [8] => eight [5] => five )

The given ARRAY (2)
Array ( [0] => 0 [1] => 3 [2] => SEVEN )


ARRAY (1) - SORT_REGULAR:
Array ( [0] => eight [1] => five [TEN] => ten )

ARRAY (2) - SORT_REGULAR:
Array ( [0] => 3 [1] => SEVEN [2] => 0 )


  3 EXERCISE   

<?php

$arrms04a 
= [ "zero" => [ "ten" => 10"eight" => ], "five" => ];

$arrms04b = [ "one" => [10 => "ten"=> "eight" ], => "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_DESCSORT_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_ASCSORT_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);

?> 

 RESULT   

ARRAY (1)
Array ( [zero] => Array ( [ten] => 10 [eight] => 8 ) [five] => 5 )

ARRAY (2)
Array ( [one] => Array ( [10] => ten [8] => eight ) [5] => five )



ARRAY (1) - SORT_DESC, SORT_REGULAR
Array ( [zero] => Array ( [ten] => 10 [eight] => 8 ) [five] => 5 )

ARRAY (2) - SORT_DESC, SORT_REGULAR
Array ( [one] => Array ( [10] => ten [8] => eight ) [0] => five )



ARRAY (2) - SORT_ASC, SORT_REGULAR
Array ( [five] => 5 [zero] => Array ( [ten] => 10 [eight] => 8 ) )

ARRAY (2) - SORT_ASC, SORT_REGULAR
Array ( [0] => five [one] => Array ( [10] => ten [8] => eight ) )


  4 EXERCISE   

<?php

$arrms05a 
= [ [ "ten" => 10"eight" => ], "five" => ]; 

$arrms05b =  [ [10 => "ten"=> "eight" ], => "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_ASCSORT_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_DESCSORT_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);

?> 

 RESULT   

ARRAY (1)
Array ( [0] => Array ( [ten] => 10 [eight] => 8 ) [five] => 5 )

ARRAY (2)
Array ( [0] => Array ( [10] => ten [8] => eight ) [5] => five )



ARRAY (1) - SORT_ASC, SORT_REGULAR
Array ( [five] => 5 [0] => Array ( [ten] => 10 [eight] => 8 ) )

ARRAY (2) - SORT_ASC, SORT_REGULAR
Array ( [0] => five [1] => Array ( [10] => ten [8] => eight ) )



ARRAY (1) - SORT_DESC, SORT_REGULAR
Array ( [0] => Array ( [ten] => 10 [eight] => 8 ) [five] => 5 )

ARRAY (2) - SORT_DESC, SORT_REGULAR
Array ( [0] => Array ( [10] => ten [8] => eight ) [1] => five )


  5 EXERCISE   

<?php

$arrms06a 
= [ 24, -5, -3]; 

$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_ASCSORT_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_DESCSORT_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);

?> 

 RESULT   

ARRAY (1)
Array ( [0] => 2 [1] => 4 [2] => -5 [3] => -3 [4] => 1 )

ARRAY (2)
Array ( [0] => two [1] => four [2] => minus five [3] => minus three [4] => one )



ARRAY (1) - SORT_ASC, SORT_STRING
Array ( [0] => -3 [1] => -5 [2] => 1 [3] => 2 [4] => 4 )

ARRAY (2) - SORT_ASC, SORT_STRING
Array ( [0] => minus three [1] => minus five [2] => one [3] => two [4] => four )



ARRAY (1) - SORT_DESC, SORT_STRING
Array ( [0] => 4 [1] => 2 [2] => 1 [3] => -5 [4] => -3 )

ARRAY (2) - SORT_DESC, SORT_STRING
rray ( [0] => four [1] => two [2] => one [3] => minus five [4] => minus three )


  6 EXERCISE   

<?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_dumparray_multisort($ar1SORT_ASCSORT_REGULAR
                                           
$ar2SORT_DESCSORT_STRING) );
echo 
'<br>';
var_dump($ar1$ar2);

echo 
"<br><br>Testing with one argument:<br>";
var_dumparray_multisort($ar2) );
echo 
'<br>';
var_dump($ar2);

?>

  7 EXERCISE   

<?php

echo "Basic functionality - 
                        renumbering of numeric keys:<br><br>"
;

// Initialise all required variables
$ar1 = array( "strkey" => 2,  1,  => 1);
$ar2 = array( 2"aa" "1");

echo 
"Testing with all normal arguments:<br>";
var_dumparray_multisort($ar1SORT_ASCSORT_REGULAR
                                    
$ar2SORT_ASCSORT_NUMERIC) );
echo 
'<br>';
var_dump($ar1$ar2);

?>

  8 EXERCISE   

<?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($bSORT_STRING$a);
echo 
'<pre>';
var_dump($a$b);
echo 
'</pre>';

?>

  10 EXERCISE   

<?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($bSORT_STRING SORT_FLAG_CASE$a);

var_dump($a$b);

?>

  11 EXERCISE   

<?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($bSORT_NATURAL$a);

var_dump($a$b);

?>

  12 EXERCISE   

<?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($bSORT_NATURAL$a);

var_dump($a$b);

?>

  13 EXERCISE   

<?php

// Something of a dummy example 
// where 0 and '0' are used as equal elements.

$ary1 array_fill(01000);
$origAry2 $ary2 array_merge(...array_fill(050, [0'0']));
array_multisort($ary1$ary2);
var_dump($ary2 === $origAry2);

?>

  14 EXERCISE   

<?php

echo "Testing with anonymous arguments:<br><br>";

var_dump(array_multisort(array(1,3,2,4)));

?>

  15 EXERCISE   

<?php

echo "Testing with empty array:<br><br>";

var_dump(array_multisort(array()));

?>

  16 EXERCISE   

<?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>';

?>

  17 EXERCISE   

<?php

echo "Testing  all array sort specifiers:<br><br>";

$ar = array( 2"aa" "1");

array_multisort($arSORT_REGULARSORT_ASC);
var_dump($ar);
echo 
'<br><br>';

array_multisort($arSORT_STRINGSORT_ASC);
var_dump($ar);
echo 
'<br><br>';

array_multisort($arSORT_NUMERICSORT_ASC);
var_dump($ar);

?>

  18 EXERCISE   

<?php

echo "Testing  all array sort specifiers:<br><br>";

$ar = array( 2"aa" "1");

array_multisort($arSORT_REGULARSORT_DESC);
var_dump($ar);
echo 
'<br><br>'

array_multisort($arSORT_STRINGSORT_DESC);
var_dump($ar);
echo 
'<br><br>'

array_multisort($arSORT_NUMERICSORT_DESC);
var_dump($ar);

?>

  19 EXERCISE   

<?php

setlocale
(LC_ALL'pt-PT''pt_PT');

$arn = [ 21.983.80.81];

array_multisort($arnSORT_LOCALE_STRING);
echo 
'<pre>';
var_dump($arn);
echo 
'</pre>'

?>