Home  >  Article  >  Backend Development  >  PHP中二维数组的排序步骤

PHP中二维数组的排序步骤

WBOY
WBOYOriginal
2016-06-13 13:04:38894browse

PHP中二维数组的排序方法


/**
* @package???? BugFree
* @version???? $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
*
*
* Sort an two-dimension array by some level two items use array_multisort() function.
*
* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
* @author????????????????????? Chunsheng Wang wwccss@263.net>
* @param? array?? $ArrayData?? the array to sort.
* @param? string? $KeyName1??? the first item to sort by.
* @param? string? $SortOrder1? the order to sort by("SORT_ASC"|"SORT_DESC")
* @param? string? $SortType1?? the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
* @return array??????????????? sorted array.
*/
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
??? if(!is_array($ArrayData))
??? {
??????? return $ArrayData;
??? }
?
??? // Get args number.
??? $ArgCount = func_num_args();
?
??? // Get keys to sort by and put them to SortRule array.
??? for($I = 1;$I
??? {
??????? $Arg = func_get_arg($I);
??????? if(!eregi("SORT",$Arg))
??????? {
??????????? $KeyNameList[] = $Arg;
??????????? $SortRule[]??? = '$'.$Arg;
??????? }
??????? else
??????? {
??????????? $SortRule[]??? = $Arg;
??????? }
??? }
?
??? // Get the values according to the keys and put them to array.
??? foreach($ArrayData AS $Key => $Info)
??? {
??????? foreach($KeyNameList AS $KeyName)
??????? {
??????????? ${$KeyName}[$Key] = $Info[$KeyName];
??????? }
??? }
?
??? // Create the eval string and eval it.
??? $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
??? eval ($EvalString);
??? return $ArrayData;
}
?
?
//################# 示例 #################
$arr = array(
??? array(
??????? 'name'??????? =>??? '学习',
??????? 'size'??????? =>??? '1235',
??????? 'type'??????? =>??? 'jpe',
??????? 'time'??????? =>??? '1921-11-13',
??????? 'class'??????? =>??? 'dd',
??? ),
??? array(
??????? 'name'??????? =>??? '中国功夫',
??????? 'size'??????? =>??? '153',
??????? 'type'??????? =>??? 'jpe',
??????? 'time'??????? =>??? '2005-11-13',
??????? 'class'??????? =>??? 'jj',
??? ),
??? array(
??????? 'name'??????? =>??? '编程',
??????? 'size'??????? =>??? '35',
??????? 'type'??????? =>??? 'gif',
??????? 'time'??????? =>??? '1997-11-13',
??????? 'class'??????? =>??? 'dd',
??? ),
??? array(
??????? 'name'??????? =>??? '中国功夫',
??????? 'size'??????? =>??? '65',
??????? 'type'??????? =>??? 'jpe',
??????? 'time'??????? =>??? '1925-02-13',
??????? 'class'??????? =>??? 'yy',
??? ),
??? array(
??????? 'name'??????? =>??? '中国功夫',
??????? 'size'??????? =>??? '5',
??????? 'type'??????? =>??? 'icon',
??????? 'time'??????? =>??? '1967-12-13',
??????? 'class'??????? =>??? 'rr',
??? ),
);
?
print_r($arr);
?
//注意:按照数字方式排序时 153 比 65 小
$temp = sysSortArray($arr,"name","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
?
print_r($temp);
?
?>

?

其他相关排序函数:

?

sort ( array &$array [, int $sort_flags] )

该函数会对目标数组进行排序,各元素按值由低到高的顺序排列.注意,该函数参数是按引用传递的, 它并不是返回排序后的数组.相反,它只是对当前数组排序,不论结果如何都不返回任何值.
注意: 本函数为 array 中的单元赋予新的键名.这将删除原有的键名而不仅是重新排序.

$arr = array(9,5,2,6,8,4,3,1); sort($arr); print_r($arr); ?>

输出:(可以看到键值关联不再保持)

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 8 [7] => 9 )

asort ( array &$array [, int $sort_flags] )

本函数对数组进行排序,数组的索引保持和单元的关联.主要用于对那些单元顺序很重要的结合数组进行排序.?同样是按引用传递的,没返回值.

<?php $arr = array(5,2,6,8,4,1); asort($arr); print_r($arr); ?>

输出(可以看到键值保持不变):

Array ( [5] => 1 [1] => 2 [4] => 4 [0] => 5 [2] => 6 [3] => 8 )

rsort ( array &$array [, int $sort_flags] )

本函数类似sort函数, 对数组进行逆向排序(最高到最低)。

注:?本函数为?array?中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

如果成功则返回?TRUE,失败则返回?FALSE

arsort ( array &$array [, int $sort_flags] )

本函数对数组进行逆向排序,数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。arsort()函数与asort()相同,只是它以相反的顺序(降序)对数组元素排序。

如果成功则返回?TRUE,失败则返回?FALSE

ksort ( array &$array [, int $sort_flags] )

ksort和sort的区别就是对数组按照键名排序而不是按值排序,保留键名到数据的关联.本函数主要用于关联数组. 如果成功则返回 TRUE,失败则返回 FALSE。

krsort ( array &$array [, int $sort_flags] )
见ksort,该函数同ksort只是把ksort后的结果逆序排序而已。

natsort ( array &$array )

本函数实现了一个和人们通常对字母数字字符串进行排序的方法一样的排序算法并保持原有键/值的关联,这被称为“自然排序”.本算法和通常的计算机字符串排序算法(用于 sort())的区别见下面示例.

如果成功则返回 TRUE,失败则返回 FALSE.
php手册中有个非常经典的例子

<?php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); sort($array1); echo "Standard sorting\n"; print_r($array1); natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?>

上例将输出:Standard sorting

Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png )

Natural order sorting

Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )

还有些数组排序函数以u(user)开头的, 都是些用户自定义排序函数。时间原因这就不多写了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn