PHP array_multisort function

Last Updated: February 14, 2022

array_multisort – sort multiple or multi dimention arrays

Syntax

 array_multisort(
    array &$array1,
    mixed $array1_sort_order = SORT_ASC,
    mixed $array1_sort_flags = SORT_REGULAR,
    mixed ...$rest
): bool

Supports (PHP 4, PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
arrays Required. list of arrays for merging
array1_sort_order

specify the sort order of the array

SORT_ASC – sort acending order

SORT_DESC –  sort decending order

array1_sort_flags

Sorting type flags:

  • SORT_REGULAR – compare items normally (don’t change types)
  • SORT_NUMERIC – compare items numerically
  • SORT_STRING – compare items as strings
  • SORT_LOCALE_STRING – compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL – compare items as strings using “natural ordering” like natsort()
  • SORT_FLAG_CASE – can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Return Value

Returns true on success or false on failure.

Example

<?php

$ar1 = array("a", "C","B");
array_multisort($ar1);
print_r($ar1);

?>

Output

Array ( [0] => B [1] => C [2] => a ) 
Test