Jump to: navigation, search

Php array multisort

From w3cyberlearnings

Contents

PHP function array_multisort

This function sorts one or more arrays and it bases on the first array. All the arrays must have the same length.

Syntax array_multisort

  • array1 : first array to be sort
  • sorting order (optional): SORT_ASC (default, sorting in ascending order A-Z), SORT_DESC (sorting in descending order Z-A)
  • sorting type (optional): SORT_REGULAR (default, compare element), SORT_NUMERIC (numeric values), SORT_STRING (string value)
  • array2 (optional): second array (based on the sorting in array1)
  • array3 (optional): third array (based on the sorting in array1)
array_multisort(array1, sorting order, sorting type, array2 , array3);

Example 1: sorting with default option

<?php

$list1 = array('banana', 'coconut', 'mango', 'apple');

array_multisort($list1);

print_r($list1);
?>


Output

Array
(
    [0] => apple
    [1] => banana
    [2] => coconut
    [3] => mango
)


Example 2: sorting with two arrays

<?php

$list1 = array('banana', 'coconut', 'mango', 'apple');
$list2 = array('lunch', 'dinner', 'breakfast','meal');

array_multisort($list1, $list2);

print_r($list1);
print_r($list2);
?>

Output

Array
(
    [0] => apple
    [1] => banana
    [2] => coconut
    [3] => mango
)
Array
(
    [0] => meal
    [1] => lunch
    [2] => dinner
    [3] => breakfast
)



Example 3: sorting with duplicated values

<?php

$list1 = array('banana', 'coconut', 'mango', 'apple', 'banana');
array_multisort($list1);

print_r($list1);
?>


Output


Array
(
    [0] => apple
    [1] => banana
    [2] => banana
    [3] => coconut
    [4] => mango
)


Example 4: merge two arrays and do the sorting

<?php

$score1 = array(30, 40, 58);
$score2 = array(40, 20, 5);

$score = array_merge($score1, $score2);

array_multisort($score, SORT_DESC, SORT_NUMERIC);

print_r($score);
?>

Output

Array
(
    [0] => 58
    [1] => 40
    [2] => 40
    [3] => 30
    [4] => 20
    [5] => 5
)

Example 5: sort string type

<?php

$list1 = array('Canana', 'Coconut', 'Mango', 'apple','APPLE');

$all_lower = array_map('strtolower', $list1);
array_multisort($all_lower, SORT_ASC, SORT_STRING, $list1);

print_r($list1);

print_r($all_lower);
?>

Output


Array
(
    [0] => APPLE
    [1] => apple
    [2] => Canana
    [3] => Coconut
    [4] => Mango
)
Array
(
    [0] => apple
    [1] => apple
    [2] => canana
    [3] => coconut
    [4] => mango
)

Example 6

<?php

$score1 = array(10, 9, 8, 7, 6, 5);
$score2 = array(100, 900, 800, 700, 600, 500000);
$score3 = array(11, 99, 88, 77, 66, 55);

array_multisort($score1, SORT_ASC, SORT_NUMERIC, $score2, $score3
);

print_r($score1);
print_r($score2);
print_r($score3);
?>

Output

Array
(
    [0] => 5
    [1] => 6
    [2] => 7
    [3] => 8
    [4] => 9
    [5] => 10
)
Array
(
    [0] => 500000
    [1] => 600
    [2] => 700
    [3] => 800
    [4] => 900
    [5] => 100
)
Array
(
    [0] => 55
    [1] => 66
    [2] => 77
    [3] => 88
    [4] => 99
    [5] => 11
)

Related Links


Navigation
Web
SQL
MISC
References