当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_multisort() 函数

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 array_multisort() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 555 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.
array_multisort()函数对数组进行分类并返回这个数组。你可以指派一个或多个数组。这个函数数个的对数组进行分类排序,如果两个或多个值相同,它自动跳到下一个数组,并对其进行分类排序。

Syntax
语法

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

Parameter
参数
Description
描述
array1 Required. Specifies an array
必要函数。指定一个函数
sorting order Optional. Specifies the sorting order. Possible values:
 可选函数。指定分类的顺序。下面列举它可能出现的值:
  • SORT_ASC Default. Sort in ascending order (A-Z)
    SORT_ASC(升序,默认值),从A-Z递增排列
  • SORT_DESC sort in descending order (Z-A)
    SORT_DESC(降序),从Z-A递减排列
sorting type Optional. Specifies the type to use, when comparing elements. Possible values:
可选函数。在与元素比较的同时,指定使用的类型。下面列举它可能出现的值:
  • SORT_REGULAR Default. Compare elements normally
    SORT_REGULAR(默认值)。比较普通元素
  • SORT_NUMERIC Compare elements as numeric values
    SORT_NUMERIC。比较数值元素
  • SORT_STRING Compare elements as string values
    SORT_STRING。比较字符串元素
array2 Optional. Specifies an array
可选函数。指定一个数组
array3 Optional. Specifies an array
可选函数。指定一个数组


Tips and Notes
提示和注意点

Tip: You can assign only one array to the array_multisort() function, or as many as you like.
提示:你可以在array_multisort()函数中指定一个或多个数组。

Note: String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1.
注意:默认使用字符串键,如果要使用数值键则需要重新指定,且从“0”开始以基数“1”递增。

Note: You can assign the sorting order and the sorting type parameters after each array. If you don't, each array parameter uses the default values.
注意:你可以指定分类顺序和分类的类型参数。如果你不指定,他们将采用默认值。


Example 1
案例1

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

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Cat [1] => Dog )
Array ( [0] => Missy [1] => Fido ) 


Example 2
案例2

See how it sorts when two values are the same:
如果两个值相同,看看她如何进行分类排序:

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

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Fido [2] => Pluto )


Example 3
案例3

With sorting parameters:
加入分类参数后的情况:

<?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);
?>

The output of the code above will be:
上述代码将输出下面的结果:

Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Pluto [2] => Fido )

评论 (0) All

登陆 | 还没注册?