doudouchan5830 2012-05-14 23:15
浏览 19
已采纳

是否有类似“array_search”的函数用于simplexml对象

I have a codeigniter app that queryies an api and gets a simplexml object. I had hard coded the values I wanted from the simplexml object, but found out later that the structure of the object isn't always the same, so I can't use casting to get the values I need.

Is there an array_search like function that I could use for this so I can use the "classid" to get the value? Here is a sampling of the simplexml object:

    [Package] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [ID] => e981b9a72cd305c7d97cc530ef9b3015
            )

        [Postage] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [CLASSID] => 3
                            )

                        [MailService] => Express Mail<sup>&reg;</sup>
                        [Rate] => 57.60
                    )

                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [CLASSID] => 2
                            )

                        [MailService] => Express Mail<sup>&reg;</sup> Hold For Pickup
                        [Rate] => 57.60
                    )

                [2] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [CLASSID] => 55
                            )

                        [MailService] => Express Mail<sup>&reg;</sup> Flat Rate Boxes
                        [Rate] => 39.95
                    )

I was using this to get the [Rate] value:

$usps->Package->Postage[0]->Rate

The problem is I need to use [CLASSID] because that is consistant where the data in the number key(?) changes.

  • 写回答

2条回答 默认 最新

  • duanjue2576 2012-05-15 00:11
    关注

    Use XPath

    You can not only search for specific elements, but do all kinds of crazy searches using various axes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端