XML到数组转换PHP

时间:2014-02-18 10:57:11

标签: php xml arrays soap

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

这是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响应。有人会给我参考网址或建议吗?

1 个答案:

答案 0 :(得分:0)

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

$xmlObject = simplexml_load_string($xmlString);

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

希望有所帮助。