Latest web development tutorials

PHP array_multisort () function

PHP Array Reference Complete PHP Array Reference

Examples

It returns an array in ascending order:

<?php
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a);
print_r($a);
?>

Running instance »

Definition and Usage

array_multisort () function returns a sorted array. You can enter one or more arrays. Function to sort an array of first, followed by other arrays, if two or more of the same value, the next will sort the array.

Note: The string key name will be retained, but the number keys will be re-index, starting at 0 and increasing by 1.

Note: You can set the sort order and sort type parameters after each array.If not set, each array will use the default parameter values.


grammar

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

参数 描述
array1 必需。规定数组。
sorting order 可选。规定排列顺序。可能的值:
  • SORT_ASC - 默认。按升序排列 (A-Z)。
  • SORT_DESC - 按降序排列 (Z-A)。
sorting type 可选。规定排序类型。可能的值:
  • SORT_REGULAR - 默认。把每一项按常规顺序排列(Standard ASCII,不改变类型)。
  • SORT_NUMERIC - 把每一项作为数字来处理。
  • SORT_STRING - 把每一项作为字符串来处理。
  • SORT_LOCALE_STRING - 把每一项作为字符串来处理,基于当前区域设置(可通过 setlocale() 进行更改)。
  • SORT_NATURAL - 把每一项作为字符串来处理,使用类似 natsort() 的自然排序。
  • SORT_FLAG_CASE - 可以结合(按位或)SORT_STRING 或 SORT_NATURAL 对字符串进行排序,不区分大小写。
array2 可选。规定数组。
array3 可选。规定数组。

technical details

return value: If successful it returns TRUE, on failure returns FALSE.
PHP version: 4+
Update log: Sort Type SORT_NATURAL and SORT_FLAG_CASE is new in PHP 5.4 in.

Sort Type SORT_LOCALE_STRING is new in PHP 5.3 in.


More examples

Example 1

It returns an array in ascending order:

<?php
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>

Running instance »

Example 2

When the two values ​​are the same sort of how:

<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>

Running instance »

Example 3

Sort parameter:

<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>

Running instance »

Example 4

Merge two arrays, according to figures in descending order:

<?php
$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference