根据深度嵌套的值对多数组进行排序

时间:2020-01-21 18:42:03

标签: sorting multidimensional-array

我正在尝试对嵌套变量上的多数组进行排序。我的数据结构显示在显示的代码下方。我想对sortby变量进行排序。该代码在一个类中。排序功能和调用代码如下所示。我认为问题在于sort函数没有找到sortby变量,但是我不知道如何解决它。

    private static function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
        $sort_col = array();
        foreach ($arr as $key=> $row) {
            $sort_col[$key] = $row[$col];
        }
        array_multisort($sort_col, $dir, $arr);
    }  

    foreach ( $controls as &$ctrl ) {
       foreach ( $ctrl as &$values ) {
           $this->array_sort_by_column($values, 'sortby');
       }
    }   

    Array
    (
            [group1] => Array 
            (
                [names] => Array
                    (
                        [0] => name_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 11
                            )

                        [1] => control_value
                    )

                [fontAwesome] => Array
                    (
                        [0] => fonts_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 1
                            )

                        [1] => control_value
                    )

                [styles] => Array
                    (
                        [0] => style_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 111
                            )

                        [1] => control_value
                    )

                [stylesAltA] => Array
                    (
                        [0] => stylesAltA_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 21
                            )

                        [1] => control_value
                    )

                [stylesAltB] => Array
                    (
                        [0] => stylesAltB_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 33
                            )

                        [1] => control_value
                    )

            )

            [group2] => Array
            (
                [names] => Array
                    (
                        [0] => name_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 15
                            )

                        [1] => control_value
                    )

                [filters] => Array
                    (
                        [0] => filters_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 17
                            )

                        [1] => control_value
                    )
            )  

            [group3] => Array
            (
                [views] => Array
                    (
                        [0] => views_entry Object
                            (
                                [value] => "abc"
                                [sortby] => 9
                            )

                        [1] => control_value
                    )
            )
    )

如果我在sort函数中显示sort_col数组,它将显示以下内容

    Array
    (
        [names] => 
        [fontAwesome] => 
        [styles] => 
        [stylesAltA] => 
        [stylesAltB] => 
        [filters] => 
        [views] => 
    )        

因此未设置值。那应该是每个对象中的[0]数组吗?任何人都可以告诉我问题出在哪里或者是否有更好的方法?

0 个答案:

没有答案