简体   繁体   中英

Sort multidimensional array of objects

I have seen so many example at stackoverflow to sort multidimensional array of object like

Sort array of objects

Sort array of objects by object fields

But none them could help me to sort my multidimensional array of objects given below

SimpleXMLElement Object
(
    [request] => SimpleXMLElement Object
        (
            [address] => test
            [citystatezip] => New York
        )

    [message] => SimpleXMLElement Object
        (
            [text] => Request successfully processed
            [code] => 0
        )

    [response] => SimpleXMLElement Object
        (
            [results] => SimpleXMLElement Object
                (
                    [result] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [zpid] => 27965224
                                    [links] => SimpleXMLElement Object
                                        (
                                            [homedetails] => test
                                            [graphsanddata] =>test
                                            [mapthishome] => test
                                            [comparables] => test
                                        )

                                    [address] => SimpleXMLElement Object
                                        (
                                            [street] => test
                                            [zipcode] => test
                                            [city] => test
                                            [state] => NY
                                            [latitude] => 29.802114
                                            [longitude] => -95.504244
                                        )

                                    [zestimate] => SimpleXMLElement Object
                                        (
                                            [amount] => 342911
                                            [last-updated] => 11/27/2014
                                            [oneWeekChange] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [deprecated] => true
                                                        )

                                                )

                                            [valueChange] => 5766
                                            [valuationRange] => SimpleXMLElement Object
                                                (
                                                    [low] => 312049
                                                    [high] => 373773
                                                )

                                            [percentile] => 0
                                        )
                                    [rentzestimate] => SimpleXMLElement Object
                                        (
                                            [amount] => 5177
                                            [last-updated] => 11/24/2014
                                            [oneWeekChange] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [deprecated] => true
                                                        )

                                                )

                                            [valueChange] => 370
                                            [valuationRange] => SimpleXMLElement Object
                                                (
                                                    [low] => 3417
                                                    [high] => 7041
                                                )

                                        )
                                    [localRealEstate] => SimpleXMLElement Object
                                        (
                                            [region] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 271582
                                                            [type] => neighborhood
                                                            [name] => test
                                                        )

                                                    [links] => SimpleXMLElement Object
                                                        (
                                                            [overview] => test
                                                            [forSaleByOwner] => test
                                                            [forSale] => test
                                                        )
                                                )
                                        )
                                )
                                [1] => SimpleXMLElement Object
                                [2] => SimpleXMLElement Object
                                [3] => SimpleXMLElement Object
                                ..............................
                                ..............................
                        )                            
                )
        )
)

I need to sort the above array on the basis of key amount in descending order. But the problem is amount key exist under two different parent keys "zestimate" and "rentzestimate".

i have tried the following function but it did not work:

public function my_comparison($a, $b) {
    if ($a->amount == $b->amount) {
            return 0;
    }
    return ($a->amount < $b->amount) ? -1 : 1;
}

Any help?

Thanks in advance

response->results->result is an array of SimpleXMLElement objects. You want to sort the array based on the inner zestimate->amount property of the element in descending order.

You have to write a comparison function that accepts SimpleXMLElement objects and, because you want a descending order, returns 1 if the zestimate->amount property of the first object is less than that of the second, -1 if it's greater and 0 if it's equal:

public function my_comparison(SimpleXMLElement $a, SimpleXMLElement $b) {
    if ($a->zestimate->amount == $b->zestimate->amount) {
        return 0;
    }
    return ($a->zestimate->amount < $b->zestimate->amount) ? 1 : -1; // note the signs
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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