加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP排序之二维数组的按照字母排序实现代码

发布时间:2020-05-24 19:04:26 所属栏目:PHP 来源:互联网
导读:PHP排序之二维数组的按照字母排序方法,在实际开发还是非常有用的,有需要的拿去

<div class="codetitle"><a style="CURSOR: pointer" data="28763" class="copybut" id="copybut28763" onclick="doCopy('code28763')"> 代码如下:<div class="codebody" id="code28763">
<?php
/*
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;
}
$ArgCount = func_num_args();
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
}
//################# 示例 #################
$arr = array(
array(
'name' => '学习',
'size' => '1235',
'type' => 'jpe',
'time' => '1921-11-13',
'class' => 'D',
),
array(
'name' => '中国功夫',
'size' => '153',
'time' => '2005-11-13',
'class' => 'J',
array(
'name' => '编程',
'size' => '35',
'type' => 'gif',
'time' => '1997-11-13',
'class' => 'A',
'size' => '65',
'time' => '1925-02-13',
'size' => '5',
'type' => 'icon',
'time' => '1967-12-13',
'class' => 'C',
);
print_r($arr);
//注意:按照数字方式排序时 153 比 65 小
$temp = sysSortArray($arr,"class","type","SORT_DESC","size","SORT_STRING");
echo "

"; 
print_r($temp);
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读