PHP SimpleXML基于其他属性值获取值

时间:2014-03-03 10:58:31

标签: php xml arrays

以下代码:

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

    )

我现在需要查询上面的数组,以根据该节点中的Name获取Address / DateTime / Text值:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用xpath表达式执行此操作,此处提取任何 <DateTimeInfo>元素,其子元素<name>的值为"PickupDate"

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

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


编辑:以下重复问题中也概述了这一点:

相关问题