Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将xml文本数组转换为关联数组_Php_Arrays_Xml - Fatal编程技术网

Php 将xml文本数组转换为关联数组

Php 将xml文本数组转换为关联数组,php,arrays,xml,Php,Arrays,Xml,我正在使用一个API,在提交一些东西后,API会向我响应一个xml文本数组: $myArray = array( "<?xml version='1.0' encoding='UTF-8'?>", "<invoices>", " <invoice>", " <name>Odin</name>", " <zip>0000</zip>", " &l

我正在使用一个API,在提交一些东西后,API会向我响应一个xml文本数组:

$myArray =  array(
    "<?xml version='1.0' encoding='UTF-8'?>",
    "<invoices>",
    "  <invoice>",
    "    <name>Odin</name>",
    "    <zip>0000</zip>",
    "    <city>Asgard</city>",
    "    <lines>",
    "      <line>",
    "        <itemNo>1</itemNo>",
    "        <qty>1.00</qty>",
    "        <prodCode>usb01T</prodCode>",
    "        <desc>USB 1TB</desc>",
    "        <unitPrice>1000.00</unitPrice>",
    "        <tax>25</tax>",
    "        <lineTaxAmount>250.00</lineTaxAmount>",
    "        <lineTotal>1250.00</lineTotal>",
    "      </line>",
    "    </lines>",
    "    <optional>",
    "      <invoiceType>ordinary</invoiceType>",
    "      <invoiceNo>9</invoiceNo>",
    "      <orderNo>119</orderNo>",
    "      <invoiceDate>07.08.15</invoiceDate>",
    "      <dueDate>21.08.15</dueDate>",
    "      <orderDate>07.08.15</orderDate>",
    "      <state>sent</state>",
    "      <recipientNo>119</recipientNo>",
    "      <address1>Valhalla</address1>",
    "      <country>NORGE</country>",
    "      <email>test@vallhalla.com</email>",
    "      <phone>000000</phone>",
    "      <yourRef>Asgard</yourRef>",
    "      <tax>250.00</tax>",
    "      <total>1250.00</total>",
    "      <accountNo>97101013352</accountNo>",
    "      <orgNo>0000000</orgNo>",
    "      <dunningFee>65.00</dunningFee>",
    "      <interestRate>9.00</interestRate>",
    "    </optional>",
    "  </invoice>","</invoices>"
);

我尝试了json_解码(json_encode($myArray),true),但失败了。还有其他方法吗?

您的问题是,就目前情况而言,您的数组只是一个行数组,而不是一个正确的XML数组,因此您的问题是如何解析它

要解决此问题,只需连接
$myArray
并将其解析为XML,然后再运行JSON编码:

$myArray = implode($myArray);
$xml = new SimpleXMLElement($myArray);
$myArray = json_decode(json_encode($xml), true));

祝你好运

谷歌:
PHP-simplexmlement
$myArray = implode($myArray);
$xml = new SimpleXMLElement($myArray);
$myArray = json_decode(json_encode($xml), true));