dongni8969 2010-09-20 16:10
浏览 33

PHP - 组合子数组并按值排序?

This is what i've got now:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 53
                    [date] => 18 Sep 2010 10:29
                    [user] => 52
                    [post] => ytiuy
                )

            [1] => Array
                (
                    [id] => 55
                    [date] => 11 Sep 2010 11:14
                    [user] => 52
                    [post] => this is a test post :]
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 56
                    [date] => 4 Sep 2010 03:19
                    [user] => 55
                    [post] => pppost :DD:D:D:D
                )

        )

)

I want to remove the first two "steps" in the array, and then sort the array by the 'date' value, like this:

Array
(
    [0] => Array
        (
            [id] => 56
            [date] => 4 Sep 2010 03:19
            [user] => 55
            [post] => pppost :DD:D:D:D
        )

    [1] => Array
        (
            [id] => 55
            [date] => 11 Sep 2010 11:14
            [user] => 52
            [post] => this is a test post :]
        )

    [2] => Array
        (
            [id] => 53
            [date] => 18 Sep 2010 10:29
            [user] => 52
            [post] => ytiuy
        )
)

Any ideas?

Thanks a bunch, appreciate all help! :)

EDIT: I should also mention that the amount of arrayitems will not always be the same.

  • 写回答

3条回答 默认 最新

  • duanbinren8906 2010-09-20 16:15
    关注

    Do you have two arrays? Or more? Are they already sorted? If so, you can use that to combine them more efficiently. If not, you probably need to sort them first.

    Roughly:

    1. Sort your input arrays (optionally)
    2. Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
    3. Repeat until all your input arrays are empty.

    Of course, if you don't care about performance at all you could simply combine all the arrays and then sort that.

    And for sorting you may want to use: http://www.php.net/manual/en/function.sort.php#99700

    @Don Kirkby: Indeed: It's basically a mergesort, but it only works on already sorted arrays. If they're both unsorted you're probably better off with combining them and using quicksort instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入