【问题标题】:XML to Array conversion PHPXML到数组转换PHP
【发布时间】:2014-02-18 10:57:11
【问题描述】:

我在将 XML 响应转换为 PHP 中的数组时遇到了一些问题。

这是 SOAP XML 响应(部分给出):

    $XMLResponse = '
<Fareportal>
<FpSearch_AirLowFaresRS>
<CntKey>1777f5a7-7824-46ce-a0f8-33d5e6e96816</CntKey>
<Currency CurrencyCode="USD"/>
<OriginDestinationOptions>
<OutBoundOptions>

<OutBoundOption segmentid="9W7008V21Feb14">
<FlightSegment FlightNumber="7008" DepartureDateTime="21Feb14T08:20 AM" ArrivalDateTime="21Feb14T09:10 AM" FlightDuration="0.50" FDType="S" StopQuantity="0" RPH="1">
<DepartureAirport LocationCode="MAA"/>
<ArrivalAirport LocationCode="BLR"/>
<Equipment AirEquipType="739"/><FlightCabin CabinType="Y"/><FlightClass ClassType="V"/><MarketingAirline Code="9W"/>
<OperatedByAirline CompanyText="JETKONNECT"/>
<Flight FlightType="O"/>
<Opaque>False</Opaque>
</FlightSegment>
</OutBoundOption>

<OutBoundOption segmentid="9W7030V21Feb2014">
<FlightSegment FlightNumber="7030" DepartureDateTime="21Feb2014T09:30 PM" ArrivalDateTime="21Feb2014T10:25 PM" FlightDuration="00.55" FDType="T" StopQuantity="0" RPH="1">
<DepartureAirport LocationCode="MAA"/>
<ArrivalAirport LocationCode="BLR"/>
<Equipment AirEquipType="739"/><FlightCabin CabinType="Y"/><FlightClass ClassType="V"/><MarketingAirline Code="9W"/>
<OperatedByAirline CompanyText="JETKONNECT"/><Flight FlightType="O"/><Opaque>False</Opaque>
</FlightSegment>
</OutBoundOption>

</OutBoundOptions>
</OriginDestinationOptions>
</FpSearch_AirLowFaresRS>
</Fareportal>
    ';

我需要获取航班详细信息,例如 FlightNumber、DepartureDateTime、ArrivalDateTime 等。

但我尝试了很多代码,如 SimpleXMLElement、DOMDocument、simplexml_load_string 所有的东西都只返回 XML 数据而不是数组。

请帮助我处理这种类型的 XML 响应。有人能给我参考网址或建议吗?

【问题讨论】:

  • 请向我们展示您尝试过的一些代码
  • 为什么你需要它作为一个数组而不是一个对象?您始终可以手动将对象转换为数组(即$a = (array) new simpleXML($xml);
  • hi tularis,对象或数组很好。
  • 我试过了,1.$doc = new DOMDocument(); $doc->loadXML($soapResponse); $Results = $doc->getElementsByTagName("searchflightavailability33result"); $Result = $Results->item(0)->nodeValue; 2.simplexml_load_string($soapResponse,'SimpleXMLElement',LIBXML_NOCDATA); 3.$xml = 新的 SimpleXMLElement($soapResponse); foreach ($xml->children() as $second_gen) { echo '
    ';print_r($second_gen);die; }

标签: php xml arrays soap


【解决方案1】:

试试 json_decode() - http://au2.php.net/manual/en/function.json-decode.php

$xmlObject = simplexml_load_string($xmlString);

$array = json_decode(json_encode($xmlObject), true);

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 2011-12-25
    • 2015-09-03
    • 2016-10-03
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多