簡體   English   中英

排序對象的多維數組

[英]Sort multidimensional array of objects

我在stackoverflow上看到了很多示例來對對象的多維數組進行排序

排序對象數組

按對象字段對對象數組進行排序

但是它們都無法幫助我對下面給出的對象的多維數組進行排序

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;
}

有什么幫助嗎?

提前致謝

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM