1

I have the following string and I want to convert it to array in PHP. Is it Possible please? of yes how can I do it? I cannot think of any way to do this in PHP. Thanks.

    $mystring = '<Service type="Q">
       <TotalNet>
          <Currency>EUR</Currency>
          <Amount>197.45</Amount>
       </TotalNet>
       <Charges>
          <Currency>EUR</Currency>
          <Charge>
             <ChargeType>MEDICAL EXPRESS</ChargeType>
             <ChargeAmount>159.19</ChargeAmount>
          </Charge>
          <Charge>
             <ChargeCode>FF</ChargeCode>
             <ChargeType>FUEL SURCHARGE</ChargeType>
             <ChargeAmount>28.26</ChargeAmount>
          </Charge>
          <Charge>
             <ChargeCode>II</ChargeCode>
             <ChargeType>SHIPMENT INSURANCE</ChargeType>
             <ChargeAmount>10.00</ChargeAmount>
          </Charge>
       </Charges>
       <DeliveryTime>2019-12-02T12:00:00</DeliveryTime>
       <CutoffTime>2019-11-26T18:00:00</CutoffTime>
       <NextBusinessDayInd>N</NextBusinessDayInd>
    </Service>
    <Service type="8">
       <TotalNet>
          <Currency>EUR</Currency>
          <Amount>72.00</Amount>
       </TotalNet>
       <Charges>
          <Currency>EUR</Currency>
          <Charge>
             <ChargeType>EXPRESS EASY</ChargeType>
             <ChargeAmount>72.00</ChargeAmount>
          </Charge>
       </Charges>
       <DeliveryTime>2019-12-02T23:59:00</DeliveryTime>
       <CutoffTime>2019-11-26T18:00:00</CutoffTime>
       <NextBusinessDayInd>N</NextBusinessDayInd>
    </Service>';
  • Ignore the accepted answer in the duplicate and go to the answer with the highest votes. – Nick Nov 28 '19 at 22:37
  • It is not working. If I use my string with the code proposed in the answer with the highest vote (395) I get an empty string. – Oussama Kamal Nov 28 '19 at 22:48
  • @OussamaKamal That's because your XML needs to have a root element. Try surrounding it with `` and `` first. – Jeto Nov 28 '19 at 23:09

0 Answers0