码迷,mamicode.com
首页 > 编程语言 > 详细

PHP 二维数组排序 可以按指定 键值排序

时间:2015-11-13 23:32:58      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

<?php
header("Content-Type:utf-8");
$arr = array(
    0 => array(
            ‘name‘ => ‘国际原油价格‘,
            ‘url_pinyin‘ => ‘kucun‘,
    ),
    1 => array(
            ‘name‘ => ‘原油‘,
            ‘url_pinyin‘ => ‘jiage‘,
    ),
    2 => array(
            ‘name‘ => ‘原油价格‘,
            ‘url_pinyin‘ => ‘yuanyoushe‘
    ),
);
print_r($arr);
// 
// PHP 二维数组排序
// 
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(!preg_match("/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;
}

print_r(sysSortArray($arr,‘name‘));

 

PHP 二维数组排序 可以按指定 键值排序

标签:

原文地址:http://www.cnblogs.com/weishang/p/4963394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!