1

我使用的是 cakephp 1.3。我试图使用 Set::sort() 函数对数组进行排序,但不工作..关于如何做到这一点的任何想法?下面是我使用的数组。

Array (
    [0] => Array
        (
            [Group] => Array
                (
                    [name] => Team A
                )

            [Members] => Array
                (
                    [0] => Array
                        (
                            [name] => George
                            [Code] => Array
                                (
                                    [name] => C
                                )

                        )

                    [1] => Array
                        (
                            [name] => Hall
                            [Code] => Array
                                (
                                    [name] => A
                                )

                        )

                    [2] => Array
                        (
                            [name] => Mike
                            [Code] => Array
                                (
                                    [name] => B
                                )

                        )

                )

        )

我使用这个对数组进行排序:

$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');

我期待这样的输出:

Array
(
    [0] => Array
        (
            [Group] => Array
                (
                    [name] => Team A
                )

            [Members] => Array
                (
                    [0] => Array
                        (
                            [name] => Hall
                            [Code] => Array
                                (
                                    [name] => A
                                )

                        )


                    [1] => Array
                        (
                            [name] => Mike
                            [Code] => Array
                                (
                                    [name] => B
                                )

                        )

                    [2] => Array
                        (
                            [name] => George
                            [Code] => Array
                                (
                                    [name] => C
                                )

                        )


                )

        )

排序不生效。我该怎么做?任何想法?

4

1 回答 1

1

仅使用Set::sort()它是不可行的。你可以这样:

    $result =  array();
    foreach($a as $arr) {
        $res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
        $result[] = array(
                    'Group' => $arr['Group'],
                    'Member' => $res
                );            
    }
    pr($result);
于 2012-04-23T17:00:59.703 回答