Home > Backend Development > PHP Tutorial > PHP two-dimensional array custom sorting

PHP two-dimensional array custom sorting

巴扎黑
Release: 2016-11-22 16:32:44
Original
1120 people have browsed it

<?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;
}
?>
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template