PHP排序之二維數組的按照字母排序方法

來源:互聯網
上載者:User

簡介:這是PHP排序之二維數組的按照字母排序方法的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=344027' scrolling='no'>PHP排序之二維數組的按照字母排序方法,

在實際開發還是非常有用的,有需要的拿去,http://lab.wehefei.com

<?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',
        'type'        =>    'jpe',
        'time'        =>    '2005-11-13',
        'class'        =>    'J',
    ),
    array(
        'name'        =>    '編程',
        'size'        =>    '35',
        'type'        =>    'gif',
        'time'        =>    '1997-11-13',
        'class'        =>    'A',
    ),
    array(
        'name'        =>    '中國功夫',
        'size'        =>    '65',
        'type'        =>    'jpe',
        'time'        =>    '1925-02-13',
        'class'        =>    'D',
    ),
    array(
        'name'        =>    '中國功夫',
        'size'        =>    '5',
        'type'        =>    'icon',
        'time'        =>    '1967-12-13',
        'class'        =>    'C',
    ),
);
 
print_r($arr);
 
//注意:按照數字方式排序時 153 比 65 小
$temp = sysSortArray($arr,"class","SORT_ASC","type","SORT_DESC","size","SORT_ASC","SORT_STRING");
 echo "<pre>";
print_r($temp);
?>

本文來源:

http://lab.wehefei.com/posts/372

來源:合肥網產品中心 http://lab.wehefei.com 更多歡迎關注下。

 

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/344027.html pageNo:5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.