在PHP排序多重阵列 [英] Sort Multi Array in PHP

查看:141
本文介绍了在PHP排序多重阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁在PHP的想法,我怎么可以排序这个数组由键(日期)?

 阵列

    [2011-02-16] =>排列
        (
            [日期] => 2011-02-16
            [NUM] => 2
        )    [2011-02-11] =>排列
        (
            [日期] => 2011-02-11
            [NUM] => 0
        )    [2011-02-17] =>排列
        (
            [日期] => 2011-02-17
            [NUM] => 0
        )    [2011-02-18] =>排列
        (
            [日期] => 2011-02-18
            [NUM] => 0
        ))


解决方案

使用 uasort 功能,这是用户自定义排序。像这样的:

 函数CMP($ A,$ B)
{
    如果($ A ['日期'] == $ B ['日期'])
    {
        返回0;
    }
    返回($ A ['日期'< $ B ['日期'])? -1:1;
}uasort($ your_array,CMP);

Does anybody have an idea how can I sort this array by key (date) in PHP?

Array
(   
    [2011-02-16] => Array
        (
            [date] => 2011-02-16
            [num] => 2
        )

    [2011-02-11] => Array
        (
            [date] => 2011-02-11
            [num] => 0
        )

    [2011-02-17] => Array
        (
            [date] => 2011-02-17
            [num] => 0
        )

    [2011-02-18] => Array
        (
            [date] => 2011-02-18
            [num] => 0
        )

)

解决方案

Use the uasort function, which is user customizable sorting. Like this:

function cmp($a, $b)
{
    if ($a['date'] == $b['date'])
    {
        return 0;
    }
    return ($a['date'] < $b['date']) ? -1 : 1;
}

uasort($your_array, "cmp");

这篇关于在PHP排序多重阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆