Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php二维数组排序(实例)

php二维数组排序(实例)

发布时间:2017-06-07   编辑:www.jquerycn.cn
分享一例php二维数组排序的例子,可以实现类似 MySQL 的 ORDER BY 效果。有需要的朋友参考下吧。

本节内容:
php中二维数组排序

一例php二维数组排序的函数,可以实现类似 MySQL 的 ORDER BY 效果,当数组不是从数据库取得时会有特殊应用。

例子:
 

复制代码 代码示例:

<?php
// 说明:PHP中二维数组的排序方法
// 编辑:www.jbxue.com
 
/**
 * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
 * @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 < $ArgCount;$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);
?>

>>> 更多内容,请查看 php数组排序方法大全 <<<

您可能感兴趣的文章:
php数组随机排序示例
php数组排序方法大全(脚本学堂整理奉献)
PHP二维数组排序的函数
php 二维数组排序的两个例子
php判断数组是否有序的示例代码
php二维数组按指定键值key排序的例子
PHP二维数组按照指定的字段排序的函数
PHP二维数组排序自定义函数
怎么用PHP实现二维数组按照键值排序
php实用快速排序算法的实例代码

关键词: php数组  二维数组排序  数组排序   
[关闭]