【问题标题】:Sort multidimensional array of objects对对象的多维数组进行排序
【发布时间】:2015-01-29 10:31:08
【问题描述】:

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

我需要根据 amount 键对上述数组进行降序排序。但问题是数量键存在于两个不同的父键“zestimate”和“rentzestimate”下。

我尝试了以下功能,但没有成功:

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

有什么帮助吗?

提前致谢

【问题讨论】:

  • 您能否澄清一下:您想对数组进行排序response-&gt;results-&gt;result
  • 请根据提供的输入示例提供结果示例。
  • 所述问题没有通用解决方案。您应该决定如何对具有矛盾数量的元素进行排序(例如,ze=&gt;1,rze=&gt;10ze=&gt;5,rze=&gt;5。)
  • 是的,我想对响应->结果->结果进行排序。我刚刚给出了数组的第零个对象的示例,并在结尾处休息。
  • 然后,正如@mudasobwa 所注意到的,您必须决定两个amount 字段如何影响最终结果,即这些字段的总和用作比较result 中的两个元素的值大批。你知道这个标准吗?

标签: php sorting object multidimensional-array


【解决方案1】:

response-&gt;results-&gt;resultSimpleXMLElement 对象的数组。您想根据元素的内部zestimate-&gt;amount 属性按降序对数组进行排序。

您必须编写一个接受SimpleXMLElement 对象的比较函数,并且由于您需要降序排列,如果第一个对象的zestimate-&gt;amount 属性小于第二个对象-1 的属性,则返回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
}

【讨论】:

  • 如何根据 zestimate->amount 对整个数组进行降序排序
  • 我更新了我的答案以澄清它。特别注意my_comparison函数的结果标志。
猜你喜欢
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2020-09-02
  • 2011-10-23
  • 2014-02-25
相关资源
最近更新 更多