user3445122
user3445122

Reputation:

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. 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. Thank you for your help.

Upvotes: 0

Views: 220

Answers (1)

jbafford
jbafford

Reputation: 5668

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. The usort callback then uses that to sort the elements in the $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.)

Upvotes: 1

Related Questions