PHP array_multisort() function

P
PHP array_merge_recursive() function
PHP array_pad() function

The array_multisort function returns a sorted array in ascending order.

When you pass multiple arrays to the array_multisort function, it sorts the first array and then follows on. If any array contains two same values, it moves to the next array and so on.

Numeric keys are reindexed, starting from 0, and increment by 1.

Note: You can specify the sorttype and sortotder after each array in the function.

What is the syntax of the array_multisort function in PHP?

array_multisort(array1, sortorder, sorttype, array2, array3, ...)
ParametersDetails
array1The array to sort – Required
sortorderSpecify the sorting order.
By default SORT_ASC – Sort in ascending order (A-Z)
SORT_DESC – Sort in descending order (Z-A) –
Optional
sorttypeSpecify the type of comparison (optional)
SORT_REGULAR – Compare elements normally – default
SORT_NUMERIC – Compare elements as numeric valuesSORT_STRING – Compare elements as string valuesSORT_LOCALE_STRING – Compare strings using current locale.
SORT_NATURAL – Compare elements as strings using “natural ordering”
SORT_FLAG_CASE – To sort strings case-insensitively Combine (bitwise OR) with SORT_STRING or SORT_NATURAL
array2 The next array to sort – Optional
array3 The next array to sort – Optional
array_multisort function in PHP

Examples of array_multisort function

<?php
$array_1=array("Horse","Lion");
$a2=array("Pak","Turk");
array_multisort($array_1,$array_2);
print_r($array_1);
print_r($a_rray2);
?>

The above example shows sorting when all values are unique.

<?php
$array_1=array("Horse","Horse","Lion");
$a2=array("Pak","Turk","USA");
array_multisort($array_1,$array_2);
print_r($array_1);
print_r($a_rray2);
?>

The above example shows sorting when there is some duplicate value.



<?php
$array_1=array("Horse","Horse","Lion");
$a2=array("Pak","Turk","USA");
array_multisort($array_1,SORT_ASC,$array_2,SORT_DESC);
print_r($array_1);
print_r($a_rray2);
?>

The above example sorts the first array in ascending order and the second array in descending order by making use of the sort type parameter.

PHP array_merge_recursive() function
PHP array_pad() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z