普通网友 2013-11-07 20:42
浏览 78
已采纳

使用php按降序排序三级嵌套数组

How to sort following array in descending order by sorder? The whole array is considered to be one array for descending sorting.I see some other questions like this but none of them helped me.

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 2208
                    [status] => u13333333333333
                    [user_id] => 6
                    [sorder] => 3

                )

            [1] => Array
                (
                    [id] => 2208
                    [status] => user111111111111111
                    [user_id] => 6
                    [sorder] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 2209
                    [status] => u222222222222222222222
                    [user_id] => 5
                    [sorder] => 2

                )

        )

)

Edit May be this is another form of the array in two dimensional.

Array
(
    [userPosts] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 2208
                            [status] => u13333333333333
                            [user_id] => 2208
                            [sorder] => 3
                        )

                    [1] => Array
                        (
                            [id] => 2208
                            [status] => user111111111111111
                            [user_id] => 2208
                            [sorder] => 1

                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 2209
                            [status] => u222222222222222222222
                            [user_id] => 2209
                            [sorder] => 2

                        )

                )

        )

)
  • 写回答

2条回答 默认 最新

  • doufud21086 2013-11-08 12:22
    关注
    $yourArray = array(
    
                    array(
                            array('id' => 2208,
                                    'status' => 'u13333333333333',
                                    'user_id' => 6,
                                    'sorder'=>3
                            ),
                            array('id' => 2208,
                                    'status' => 'user111111111111111',
                                    'user_id' => 6,
                                    'sorder'=>1
                            ),
                    ),
                    array(
                            array('id' => 2209,
                                    'status' => 'u222222222222222222222',
                                    'user_id' => 5,
                                    'sorder'=>2
                            ),
    
                    ),
    
    
    );
    /*Merger arrays as one */
    function loopArrayMerger(array $bigArray) {
        if (!$bigArray) {
            return array();
        }
    
        return call_user_func_array('array_merge', $bigArray);
    }
    $flatedArray = loopArrayMerger($yourArray);
    /*Call back function for sorting, if you want in ascending order just replace 1:-1 with -1:1*/
    function compareElementsInArray($a, $b) {
        if ($a['sorder'] == $b['sorder']) {
            return 0;
        }
        return ($a['sorder'] < $b['sorder']) ? 1 : -1;
    }
    
    /*Finally sort your array*/
    uasort($flatedArray, 'compareElementsInArray');
    echo '<pre>';
    print_r($flatedArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?