PHP排序数组通过子数组值 [英] PHP Sort Array By SubArray Value

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

问题描述

我有数组的下structue:

 阵列
        (
            [0] =>排列
                (
                    [configuration_id] => 10
                    [ID] => 1
                    [OPTIONNUMBER] => 3
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )            [1] =>排列
                (
                    [configuration_id] => 9
                    [ID] => 1
                    [OPTIONNUMBER] => 2
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )            [2] =>排列
                (
                    [configuration_id] => 8
                    [ID] => 1
                    [OPTIONNUMBER] => 1
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )
    )

什么订单是最好的方式排列,以增量的方式基础上,OPTIONNUMBER?

因此​​,结果如下:

 阵列
        (
            [0] =>排列
                (
                    [configuration_id] => 8
                    [ID] => 1
                    [OPTIONNUMBER] => 1
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )            [1] =>排列
                (
                    [configuration_id] => 9
                    [ID] => 1
                    [OPTIONNUMBER] => 2
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )            [2] =>排列
                (
                    [configuration_id] => 10
                    [ID] => 1
                    [OPTIONNUMBER] => 3
                    [optionActive] => 1
                    [LASTUPDATED] => 2010-03-17 15点44分十二秒
                )
    )


解决方案

使用 usort

 函数cmp_by_optionNumber($ A,$ B){
  返回$一个[OPTIONNUMBER] - $ B [OPTIONNUMBER];
}...usort($阵列cmp_by_optionNumber);

I've got the following structue of array:

Array
        (
            [0] => Array
                (
                    [configuration_id] => 10
                    [id] => 1
                    [optionNumber] => 3
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )

            [1] => Array
                (
                    [configuration_id] => 9
                    [id] => 1
                    [optionNumber] => 2
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )

            [2] => Array
                (
                    [configuration_id] => 8
                    [id] => 1
                    [optionNumber] => 1
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )
    )

What's the best way for order the array, in an incremental way based on the optionNumber?

So the results look like:

Array
        (
            [0] => Array
                (
                    [configuration_id] => 8
                    [id] => 1
                    [optionNumber] => 1
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )

            [1] => Array
                (
                    [configuration_id] => 9
                    [id] => 1
                    [optionNumber] => 2
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )

            [2] => Array
                (
                    [configuration_id] => 10
                    [id] => 1
                    [optionNumber] => 3
                    [optionActive] => 1
                    [lastUpdated] => 2010-03-17 15:44:12
                )
    )

解决方案

Use usort.

function cmp_by_optionNumber($a, $b) {
  return $a["optionNumber"] - $b["optionNumber"];
}

...

usort($array, "cmp_by_optionNumber");

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

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