排序关联数组由两个值PHP [英] sort associative array by two values PHP

查看:191
本文介绍了排序关联数组由两个值PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组一些预订数据,他们已经在排序[英],但我也希望通过[AMPM]排序,所以每个组[ENG]为[AMPM]排序。有谁知道如何在PHP中做到这一点?

 阵列

    [XML] =>排列
        (
            [预订] =>排列
                (
                    [0] =>排列
                    (                        [日期] => 29/12/10
                        [英] => ALS
                        [AMPM] =>上午
                        [时间] => 2.00
                        [类型] = GT;小号
                        [SEQ] =→2
                        [时间] => 0                    )                [1] =>排列
                    (                        [日期] => 29/12/10
                        [英] => BDS
                        [AMPM] =>下午
                        [时间] => 2.30
                        [类型] = GT;小号
                        [SEQ] => 3
                        [时间] => 0                    )


解决方案

您可以使用usort:的 http://www.php.net/manual/en/function.usort.php

所以,当从两个项目是不同的,你可以返回1或-1相应的,但是如果都是一样的,那么你可以比较 AMPM 返回0,1或-1相应。

i have some booking data in an array, they are already sorted by [eng], but I also want to sort by [ampm], so each group of [eng] is sorted by [ampm]. Does anybody know how to accomplish this in php?

    Array
(
    [xml] => Array
        (
            [booking] => Array
                (
                    [0] => Array
                    (

                        [date] => 29/12/10
                        [eng] => ALS
                        [ampm] => AM
                        [time] => 2.00
                        [type] => S
                        [seq] =>2
                        [duration] => 0

                    )

                [1] => Array
                    (

                        [date] => 29/12/10
                        [eng] => BDS
                        [ampm] => PM
                        [time] =>       2.30
                        [type] => S
                        [seq] =>       3
                        [duration] =>       0

                    )

解决方案

you can use usort: http://www.php.net/manual/en/function.usort.php

so that when eng from the two items are different, you can return 1 or -1 accordingly, but if eng are the same, then you can compare ampm to return 0, 1, or -1 accordingly.

这篇关于排序关联数组由两个值PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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