PHP Programing language

adplus-dvertising
array_multisort() Function in PHP
Previous Home Next
adplus-dvertising

array_multisort()

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the another arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

Syntax:
array_multisort(array1,sorting order,sorting type,array2,array3...)

In above syntax, "array1" specifies an array, "sorting order" specifies the sorting order and values are as follows:

  • SORT_ASC: Sort in ascending order and is default order (A-Z)
  • SORT_DESC: Sort in descending order (Z-A)
"sorting type" specifies the type to use, when comparing elements and values are as follows:
  • SORT_REGULAR: Compare elements normally (Standard ASCII) and is default type
  • SORT_NUMERIC: Compare elements as numeric values
  • SORT_STRING: Compare elements as string values
  • SORT_LOCALE_STRING: Compare elements as string, based on the current locale (can be changed using setlocale())
  • SORT_NATURAL: Compare elements 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
"array2" specifies an array and "array3" specifies an array

Example:

<?php
$array1=array("Apple","Banana","Orange");
$array2=array("Onion","Tomato","Potato");
array_multisort($array1,$array2);
print_r($array1);
print_r($array2);
?>

Output:

Array ( [0] => Apple [1] => Banana [2] => Orange ) Array ( [0] => Onion [1] => Tomato [2] => Potato )

Previous Home Next