简体   繁体   English

根据php中的嵌套键对对象数组进行排序

[英]Sorting the array of objects based on nested key in php

I have Dealer entity in my database and each dealer posses some makes of cars. 我的数据库中有代理商实体,每个代理商都拥有一些汽车品牌。 Car makes have different prices based on their feature set. 汽车制造商根据其功能集有不同的价格。 Below are given only two records of the dealer object. 下面仅给出经销商对象的两个记录。 I want to sort the dealers with lowest prices first. 我想首先对价格最低的经销商进行排序。 How can I do that? 我怎样才能做到这一点?

Dealer Object
    (
        [name] => Kosh Motors
        [address] => NYC
        [make] => Array
            (
                [0] => Make Object
                    (
                        [makeName] => Mercedes
                        [Prices] => Array
                            (
                                [0] =>Price Object
                                    (
                                        [makeDescription] => Some description here
                                        [price] => 12400
                                    )

                                [1] =>Price Object
                                    (
                                        [sDescription] => Variant with more features
                                        [price] => 16600

                                    )

                            )

                    )

            )

    )
    Dealer Object
    (
        [name] => Majesty Motors
        [address] => London, United Kingdom
        [make] => Array
            (
                [0] => Make Object
                    (
                        [makeName] => BMW
                        [Prices] => Array
                            (
                                [0] =>Price Object
                                    (
                                        [makeDescription] => Some description here
                                        [price] => 6400
                                    )

                                [1] =>Price Object
                                    (
                                        [sDescription] => Variant with more features
                                        [price] => 8700

                                    )

                            )

                    )

            )

    )

I have tried the usort but its not working account to my requirement. 我已经尝试过usort,但是它不能满足我的要求。 Actually this nesting is too complex for me to handle. 实际上,这种嵌套对于我来说太复杂了。 I want that Majesty Motors Dealer to appear first after the sort because it has lower prices than Kosh Motors. 我希望Majesty Motors Dealer在排序之后首先出现,因为它的价格比Kosh Motors低。 Thank you for your help. 谢谢您的帮助。

So, in your case where the sorting is based on the single lowest absolute price a dealer has, your sort function would look something like this: 因此,在您根据交易商拥有的单个最低绝对价格进行排序的情况下,您的排序功能将如下所示:

<?php

function lowestPriceForDealer($dealer)
{
    $lowest = -1;
    foreach($dealer->make as $makes) {
        foreach($makes->Prices as $price) {
            if($lowest == -1)
                $lowest = $price;
            else if($price < $lowest)
                $lowest = $price;
        }
    }

    return $lowest;
}

usort($arr, function($a, $b) {
    return lowestPriceForDealer($a) <=> lowestPriceForDealer($b);
});

This is not the most efficient way of doing it, but it will work. 这不是最有效的方法,但是会起作用。

lowestPriceForDealer returns the single lowest price for a dealer. lowestPriceForDealer返回经销商的单个最低价格。 The usort callback then uses that to sort the elements in the $arr . 然后, usort回调使用该回调对$arr的元素进行排序。

(A more efficient implementation would either pre-calculate the lowest price for each dealer, or update the dealer object with their minimum price, and then sort based on that, instead of re-calculating the minimum price at each step in the sorting process. If there is a large number of prices in your input array, you will want to do this.) (一种更有效的实现方式是预先计算每个经销商的最低价格,或者用其最低价格更新经销商对象,​​然后基于此价格进行分类,而不是在分类过程的每个步骤中重新计算最低价格。如果您的输入数组中有大量价格,您将需要这样做。)

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

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