对多维对象数组进行排序

时间:2014-12-01 12:06:34

标签: php sorting object multidimensional-array

我在stackoverflow上看到了很多这样的例子来对像

这样的对象的多维数组进行排序

Sort array of objects

Sort array of objects by object fields

但是没有人可以帮我分类下面给出的多维对象数组

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
                                ..............................
                                ..............................
                        )                            
                )
        )
)

我需要根据键金额按降序对上面的数组进行排序。但问题是金额密钥存在于两个不同的父密钥之下" zestimate"和" rentzestimate"。

我尝试过以下功能,但不起作用:

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

任何帮助?

提前致谢

1 个答案:

答案 0 :(得分:2)

response->results->resultSimpleXMLElement个对象的数组。您希望根据元素的内部zestimate->amount属性按降序对数组进行排序。

您必须编写一个接受SimpleXMLElement个对象的比较函数,并且因为您希望降序,如果第一个对象的1属性小于zestimate->amount属性,则返回-1第二个,0如果它更大,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 } 如果它相等:

{{1}}