php二维数组自定义排序

巴扎黑
发布: 2016-11-22 16:32:44
原创
1096 人浏览过

<?php
//对二维数组自定义排序
function array_sort($arr,$keys,$type){
    $keysvalue=array();
    $i = 0;
    foreach($arr as $key=>$val) {
        $val[$keys] = str_replace("-","",$val[$keys]);
        $val[$keys] = str_replace(" ","",$val[$keys]);
        $val[$keys] = str_replace(":","",$val[$keys]);
        $keysvalue[] =$val[$keys];
    }
    asort($keysvalue); //key值排序
    reset($keysvalue); //指针重新指向数组第一个
    foreach($keysvalue as $key=>$vals) {
        $keysort[] = $key;
    }
    $new_array = array();
    if($type != "asc"){
        for($ii=count($keysort)-1; $ii>=0; $ii--) {
            $new_array[] = $arr[$keysort[$ii]];
        }
    }else{
        for($ii=0; $ii<count($keysort); $ii++){
        $new_array[] = $arr[$keysort[$ii]];
        }
    }
    return $new_array;
}
$arr = array(
        array(
                &#39;name&#39;        =>    &#39;学习&#39;,
                &#39;size&#39;        =>    &#39;1235&#39;,
                &#39;type&#39;        =>    &#39;jpe&#39;,
                &#39;time&#39;        =>    &#39;1921-11-13&#39;,
                &#39;class&#39;        =>    &#39;dd&#39;,
        ),
        array(
                &#39;name&#39;        =>    &#39;中国功夫&#39;,
                &#39;size&#39;        =>    &#39;153&#39;,
                &#39;type&#39;        =>    &#39;jpe&#39;,
                &#39;time&#39;        =>    &#39;2005-11-13&#39;,
                &#39;class&#39;        =>    &#39;jj&#39;,
        ),
        array(
                &#39;name&#39;        =>    &#39;编程&#39;,
                &#39;size&#39;        =>    &#39;35&#39;,
                &#39;type&#39;        =>    &#39;gif&#39;,
                &#39;time&#39;        =>    &#39;1997-11-13&#39;,
                &#39;class&#39;        =>    &#39;dd&#39;,
        ),
        array(
                &#39;name&#39;        =>    &#39;中国功夫&#39;,
                &#39;size&#39;        =>    &#39;65&#39;,
                &#39;type&#39;        =>    &#39;jpe&#39;,
                &#39;time&#39;        =>    &#39;1925-02-13&#39;,
                &#39;class&#39;        =>    &#39;yy&#39;,
        ),
);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r(array_sort($arr,&#39;size&#39;,&#39;asc&#39;));
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[]    = &#39;$&#39;.$Arg;
        }
        else
        {
            $SortRule[]    = $Arg;
        }
    }
    foreach($ArrayData AS $Key => $Info)
    {
        foreach($KeyNameList AS $KeyName)
        {
            ${$KeyName}[$Key] = $Info[$KeyName];
        }
    }
    $EvalString = &#39;array_multisort(&#39;.join(",",$SortRule).&#39;,$ArrayData);&#39;;
    eval ($EvalString);
    return $ArrayData;
}
?>
登录后复制


相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板