>백엔드 개발 >PHP 튜토리얼 >PHP 2차원 배열 사용자 정의 정렬

PHP 2차원 배열 사용자 정의 정렬

巴扎黑
巴扎黑원래의
2016-11-22 16:32:441114검색

<?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;
}
?>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.