php中的数组到XML转换

时间:2014-05-22 14:53:36

标签: php arrays xml

在我的航空预订项目中,我想将空中搜索请求响应数组转换为XML 空中预订特定格式的请求我想将下面的数组转换为格式化的XML 格式如下

我使用此代码将php数组转换为xml

我的数组

Array
(
    [0] => Array
        (
            [FareDetails] => Array
                (
                    [ActualBaseFare] => 12375
                    [Tax] => 21005
                    [STax] => 0
                    [TCharge] => 0
                    [SCharge] => 0
                    [TDiscount] => 0
                    [TMarkup] => 0
                    [TPartnerCommission] => 0
                    [TSdiscount] => 0
                    [FareBreakup] => Array
                        (
                            [FareAry] => Array
                                (
                                    [Fare] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [PsgrType] => ADT
                                                    [BaseFare] => 4500
                                                    [Tax] => 7020
                                                    [TaxDataAry] => Array
                                                        (
                                                            [TaxData] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [Country] => JN
                                                                            [Amt] => 00000528
                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [Country] => WO
                                                                            [Amt] => 00000233
                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [Country] => ZR
                                                                            [Amt] => 00000085
                                                                        )

                                                                    [3] => Array
                                                                        (
                                                                            [Country] => YR
                                                                            [Amt] => 00000300
                                                                        )

                                                                    [4] => Array
                                                                        (
                                                                            [Country] => Other
                                                                            [Amt] => 0
                                                                        )

                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [PsgrType] => CNN
                                                    [BaseFare] => 3375
                                                    [Tax] => 6965
                                                    [TaxDataAry] => Array
                                                        (
                                                            [TaxData] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [Country] => JN
                                                                            [Amt] => 00000473
                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [Country] => WO
                                                                            [Amt] => 00000233
                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [Country] => ZR
                                                                            [Amt] => 00000085
                                                                        )

                                                                    [3] => Array
                                                                        (
                                                                            [Country] => YR
                                                                            [Amt] => 00000300
                                                                        )

                                                                    [4] => Array
                                                                        (
                                                                            [Country] => Other
                                                                            [Amt] => 0
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [ocTax] => 0
                )

        )

)

我们希望使用以下格式将数组上面的数据转换为XML

<OriginDestinationOption>
        <FareDetails>
          <ActualBaseFare>12375</ActualBaseFare>
          <Tax>21005</Tax>
          <STax>0</STax>
          <TCharge>0</TCharge>
          <SCharge>0</SCharge>
          <TDiscount>0</TDiscount>
          <TMarkup>0</TMarkup>
          <TPartnerCommission>0</TPartnerCommission>
          <TSdiscount>0</TSdiscount>
          <FareBreakup>
            <FareAry>
              <Fare>
                <PsgrType>ADT</PsgrType>
                <BaseFare>4500</BaseFare>
                <Tax>7020</Tax>
                <TaxDataAry>
                  <TaxData>
                    <Country>JN</Country>
                    <Amt>00000528</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>WO</Country>
                    <Amt>00000233</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>ZR</Country>
                    <Amt>00000085</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>YR</Country>
                    <Amt>00000300</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>Other</Country>
                    <Amt>0</Amt>
                  </TaxData>
                </TaxDataAry>
              </Fare>
              <Fare>
                <PsgrType>CNN</PsgrType>
                <BaseFare>3375</BaseFare>
                <Tax>6965</Tax>
                <TaxDataAry>
                  <TaxData>
                    <Country>JN</Country>
                    <Amt>00000473</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>WO</Country>
                    <Amt>00000233</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>ZR</Country>
                    <Amt>00000085</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>YR</Country>
                    <Amt>00000300</Amt>
                  </TaxData>
                  <TaxData>
                    <Country>Other</Country>
                    <Amt>0</Amt>
                  </TaxData>
                </TaxDataAry>
              </Fare>
            </FareAry>
          </FareBreakup>
          <ocTax>0</ocTax>
        </FareDetails>

      </OriginDestinationOption>

0 个答案:

没有答案