【问题标题】:PHP SimpleXML get value based on other property value [duplicate]PHP SimpleXML根据其他属性值获取值[重复]
【发布时间】:2014-03-03 10:58:31
【问题描述】:

以下代码:

 // Get sub category:
 $xml = simplexml_load_string($update_booking_info->XML);

给我这个:(到目前为止都正确)

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

[LocationClass] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Type] => LocationIdentification
                    )

                [Lat] => 0
                [Long] => 0
                [VZeroQuestionLifeTimeKey] => SimpleXMLElement Object
                    (
                    )

                [VZeroQuestionsCollectionID] => SimpleXMLElement Object
                    (
                    )

                [Name] => FromLocation
                [Address] => Some address 1
            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Type] => LocationIdentification
                    )

                [Lat] => 0
                [Long] => 0
                [VZeroQuestionLifeTimeKey] => SimpleXMLElement Object
                    (
                    )

                [VZeroQuestionsCollectionID] => SimpleXMLElement Object
                    (
                    )

                [Name] => ToLocation
                [Address] => Some Address 2
            )

    )

[DateTimeInfo] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [Type] => DateTime
            )

        [Lat] => 0
        [Long] => 0
        [VZeroQuestionLifeTimeKey] => 3842
        [VZeroQuestionsCollectionID] => 916
        [Name] => PickupDate
        [DateTime] => 2014-03-28 00:59:08
    )

[TextField] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [Lat] => 0
                [Long] => 0
                [VZeroQuestionLifeTimeKey] => 3846
                [VZeroQuestionsCollectionID] => 916
                [Name] => Cellphone
                [Text] => 1234567891
            )

        [1] => SimpleXMLElement Object
            (
                [Lat] => 0
                [Long] => 0
                [VZeroQuestionLifeTimeKey] => 3843
                [VZeroQuestionsCollectionID] => 916
                [Name] => ContactName
                [Text] => John Smit
            )

        [2] => SimpleXMLElement Object
            (
                [Lat] => 0
                [Long] => 0
                [VZeroQuestionLifeTimeKey] => 3848
                [VZeroQuestionsCollectionID] => 916
                [Name] => NumberPassengers
                [Text] => 2
            )

    )

)

我现在需要根据该节点内的名称查询上面的数组以获取地址/日期时间/文本值:

因此,如果我要查找来自 DateTimeInfo 的 DateTime 值,我需要查询唯一的名称 (PickupDate) 并获取相关属性。我已经有一个变量,它将告诉我该节点的“值”属性是什么(如 DateTime、Text 等),所以我只需要知道如何查询 XML 以获取正确的节点并在该节点中查找值。如果需要,我也很乐意使用其他阅读器。

有什么想法吗?

【问题讨论】:

  • 你可以用一个叫做 XPath 的东西来做这件事。它在 SimpleXML SimpleXMLElement::xpath() 中可用。对于您的问题,在 SimpleXML 中,发布 print_r / var_dump 的输出没有用。而是逐字提供 XML,以便清楚您操作的是哪个 XML。如果它很大,则为您的示例将其缩小。

标签: php xml arrays


【解决方案1】:

您可以使用 xpath 表达式执行此操作,此处获取 any <DateTimeInfo> 元素,该元素有一个子 <name>,其值为 "PickupDate"

$xpath = '//DateTimeInfo[name="PickupDate"]/DateTime';
list ($first) = $xml->xpath($xpath);

请参阅 SimpleXMLElement::xpath()Basic SimpleXML usage


编辑:以下重复问题也对此进行了概述:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-07-09
    • 2012-05-19
    相关资源
    最近更新 更多