0

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

Community
  • 1
  • 1
Neeraj
  • 8,625
  • 18
  • 60
  • 89
  • Could you clarify please: you want to sort an array `response->results->result`? – Alexander Guz Dec 01 '14 at 12:11
  • Please provide an example of your result based upon the supplied input example. – RichardBernards Dec 01 '14 at 12:12
  • The stated problem does not have a general solution. You should decide how you want to sort elements with contradicting amounts (say, `ze=>1,rze=>10` vs `ze=>5,rze=>5`.) – Aleksei Matiushkin Dec 01 '14 at 12:12
  • Yes, i want to sort response->results->result. I just have given example of Zeroth object of array and rest at are at the end. – Neeraj Dec 01 '14 at 12:14
  • Then, as @mudasobwa noticed, you have to decide how both `amount` fields influence the final result, i.e. the sum of these fields is used as a value for comparing two elements in the `result` array. Do you know this criteria? – Alexander Guz Dec 01 '14 at 12:17
  • I actually want to sort response->results->result array on the basis of response->results->result->zestimate->amount value. This amount does not have to do anything with response->results->result->rentzestimate->amount. – Neeraj Dec 01 '14 at 12:18

1 Answers1

2

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
}
Alexander Guz
  • 1,334
  • 12
  • 31