doumengxue7371 2010-06-02 12:21
浏览 45
已采纳

PHP关联数组排序

I have an array like one below. Currently it is sorted alphabetically by the OwnerNickName field. Now i want to brig the array entry with OwnerNickName 'My House' as the first entry of the array and rest sorted alphabetically by OwnerNickName. Any idea?

Array
(
    [0318B69D-5DEB-11DF-9D7E-0026B9481364] => Array
        (
            [OwnerNickName] => andy
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Living Room
                            [RoomKey] => FC795A73-695E-11DF-9D7E-0026B9481364
                        )

                )

        )

    [286C29DE-A9BE-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => anton
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => KidsRoom
                            [RoomKey] => E79D7991-64DC-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => CC12C0C4-68AA-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 67A280D4-64D9-11DF-9D7E-0026B9481364
                        )

                )

        )

    [8BE18F84-AC22-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => mike
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 1C6AFB39-6835-11DF-9D7E-0026B9481364
                        )

                )

        )

    [29B455DE-A9BC-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => My House
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => 61ECFAB2-6376-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Rec Room
                            [RoomKey] => 52B8B781-6376-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Deck
                            [RoomKey] => FFEB4102-64DE-11DF-9D7E-0026B9481364
                        )

                    [3] => Array
                        (
                            [Label] => My Room2
                            [RoomKey] => 112473E4-64DF-11DF-9D7E-0026B9481364
                        )

                    [4] => Array
                        (
                            [Label] => Bar Room
                            [RoomKey] => F82C47A8-64DE-11DF-9D7E-0026B9481364
                        )

                )

        )

)
  • 写回答

2条回答 默认 最新

  • dreamfly0514 2010-06-02 12:27
    关注

    You may want to implement your own sorting function, e.g.:

    function cmp($a, $b)
    {
        if ($a['OwnerNickName'] == $b['OwnerNickName']) {
            return 0;
        }
        if ($a['OwnerNickName'] == 'My House') {
            return -1;
        } else if ($b['OwnerNickName'] == 'My House') {
            return 1;
        }
        return ($a['OwnerNickName'] < $b['OwnerNickName']) ? -1 : 1;
    }    
    usort($array, 'cmp');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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