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

6
  • Could you clarify please: you want to sort an array response->results->result? Commented Dec 1, 2014 at 12:11
  • Please provide an example of your result based upon the supplied input example. Commented Dec 1, 2014 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.) Commented Dec 1, 2014 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. Commented Dec 1, 2014 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? Commented Dec 1, 2014 at 12:17

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

how would it sort the entire array in desecding order on the basis of zestimate->amount
I updated my answer to clarify it. Pay special attention to the signs of the result of my_comparison function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.