简体   繁体   English

按与stdClass对象组合的值对多维数组进行排序

[英]Sort multidimensional array by value that is combinded with stdClass Object

Fund few functions to sort array but none of them can sort them if they are stdClass Object or ad least I could not find the right one 资助一些函数来对数组进行排序,但是如果它们是stdClass Object或至少我找不到合适的函数,则它们都无法对它们进行排序

I need to sort this array by ordering value 我需要通过排序值对该数组进行排序

 [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 12
                            [name] => Title1
                            [ordering] => 1
                        )

                    [1] => stdClass Object
                        (
                            [id] => 14
                            [name] => Title2
                            [ordering] => 3
                        )

                    [2] => stdClass Object
                        (
                            [id] => 18
                            [name] => Title4
                            [ordering] => 2
                        )

                    [3] => stdClass Object
                        (
                            [id] => 22
                            [name] => Title5
                            [ordering] => 4
                        )

                )

any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

Define a custom sorting function based on ordering field of your objects, then pass it in with usort. 根据对象的ordering字段定义自定义排序功能,然后将其与usort一起传递。

docs for usort docs for usort

function sortByOrdering($obj1, $obj2) {
   return $obj2->ordering - $obj1->ordering;
}

usort($input['data'], 'sortByOrdering');

Note that you don't need to reassign $input = usort(...) as the array is referenced inside usort 请注意,您不需要重新分配$input = usort(...)因为在usort内部引用了数组

Thanks, SiGanteng! 谢谢,斯甘藤! That's just what I was looking for. 那就是我想要的。

I noted that the resulting sort order was descending. 我注意到结果排序顺序是降序的。 I was able to reverse that with a simple change to your function - I swapped $obj1 and $obj2 on the return statement: 我可以通过对函数进行简单的更改来扭转这种情况-我在return语句上交换了$ obj1和$ obj2:

return $obj1->ordering - $obj2->ordering;

In my application, the array data was: 在我的应用程序中,数组数据为:

Array
(
    [3] => stdClass Object
        (
            [term_id] => 3
            [name] => Salesperson
            [slug] => 01-salesperson
            [term_group] => 0
            [term_order] => 0
            [term_taxonomy_id] => 3
            [taxonomy] => skill
            [description] => 
            [parent] => 0
            [count] => 6
            [object_id] => 53
        )

    [5] => stdClass Object
        (
            [term_id] => 5
            [name] => Airport Security
            [slug] => 02-airport-security
            [term_group] => 0
            [term_order] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => skill
            [description] => 
            [parent] => 0
            [count] => 30
            [object_id] => 575
        )

    [4] => stdClass Object
        (
            [term_id] => 4
            [name] => Baker
            [slug] => 03-baker
            [term_group] => 0
            [term_order] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => skill
            [description] => 
            [parent] => 0
            [count] => 28
            [object_id] => 453
        )
)

Using the above data, I needed to resort array $skills_nav in ascending order by "slug" value. 使用以上数据,我需要按“ slug”值升序排列数组$ skills_nav。 Here's the final code which accomplished the task: 这是完成任务的最终代码:

function sortByOrdering($obj1, $obj2) {
   return $obj1->slug - $obj2->slug;
}
usort($skills_nav, 'sortByOrdering');

To further explain to the reader, the two arguments expected by function sortByOrdering are automatically supplied by USORT. 为了进一步向读者解释,USORT自动提供了sortByOrdering函数期望的两个参数。 This is explained by the USORT function in the manual. 手册中的USORT功能对此进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM