PHP array_multisort() Function
In this chapter you will learn:
- Definition for PHP array_multisort() Function
- Syntax for PHP array_multisort() Function
- Parameter for PHP array_multisort() Function
- Example - Using sorting parameters
- Example - Merge two arrays and sort them as numbers, in descending order
- Example - array_multisort keeps the relationship between arrays
- Example - Use array_multisort() to sort multidimensional arrays
Definition
array_multisort() sorts multiple related arrays at the same time, preserving the relationship between the arrays.
String keys will be maintained, but numeric keys will be re-indexed.
Syntax
PHP array_multisort() Function has the following syntax.
array_multisort(array1,sortOrder,sortType,array2,array3...)
Parameter
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | Array to sort |
sortOrder | Optional. | Sort Order. |
sortType | Optional. | Type to use to compare elements. |
array2 | Optional. | Array to sort |
array3 | Optional. | Array to sort |
Possible values for sortOrder:
Value | Description |
---|---|
SORT_ASC | Default. Sort in ascending order (A-Z) |
SORT_DESC | Sort in descending order (Z-A) |
Possible values for sortType:
Value | Description |
---|---|
SORT_REGULAR | Default. Compare elements as ASCII value |
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 which 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 |
Example 1
Using sorting parameters:
<?php/*from java 2 s . com*/
$a1=array("P","J","H");
$a2=array("PHP","Java","HTML");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>
The code above generates the following result.
Example 2
Merge two arrays and sort them as numbers, in descending order:
<?php/*from j a v a 2 s . c om*/
$a1=array(1,3,5,7,9);
$a2=array(4,3,2,1,6);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>
The code above generates the following result.
Example 3
The following script stores book information in three related arrays. By passing all three arrays to array_multisort(), the arrays are all sorted according to the values in the first array:
<?PHP/*j av a2 s . c o m*/
$authors = array( "Java", "PHP", "CSS", "HTML" );
$titles = array( "Learn Java from java2s.com", "Learn PHP from java2s.com", "Learn CSS from java2s.com", "Learn HTML from java2s.com" );
$pubYears = array( 2000, 2001, 2002, 2003 );
array_multisort( $authors, $titles, $pubYears );
print_r ( $authors );
print "\n";
print_r ( $titles );
print "\n";
print_r ( $pubYears );
print "\n";
array_multisort( $titles, $authors, $pubYears );
print_r ( $authors );
print "\n";
print_r ( $titles );
print "\n";
print_r ( $pubYears );
print "\n";
?>
The code above generates the following result.
Example 4
Use array_multisort() to sort multidimensional arrays.
The function then sorts the array by the first element of each nested array, then by the second element of each nested array, and so on.
The order of the elements in the nested array is untouched.
<?php //from ja va 2 s. c om
$myBooks = array(
array(
"title" => "Learn PHP from java2s.com",
"author" => "java2s.com",
"pubYear" => 2000
),
array(
"title" => "Oracle",
"author" => "java2s.com",
"pubYear" => 1962
),
array(
"title" => "Learn Java from java2s.com",
"author" => "JavaAuthor",
"pubYear" => 2001
),
array(
"title" => "Learn HTML from java2s.com",
"author" => "HTMLAuthor",
"pubYear" => 2002
),
array(
"title" => "Learn CSS from java2s.com",
"author" => "CSSAuthor",
"pubYear" => 2003
),
);
array_multisort( $myBooks );
print_r( $myBooks );
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_pad() Function
- Syntax for PHP array_pad() Function
- Parameter for PHP array_pad() Function
- Example - Return 5 elements and insert a value of "blue" to the new elements in the array
- Example - Using a negative size parameter