Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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_Xml_Zend Framework - Fatal编程技术网

Php 将XML转换为关联数组

Php 将XML转换为关联数组,php,xml,zend-framework,Php,Xml,Zend Framework,有没有一种方法可以使用Zend Framework和PHP将XML转换为数组?我见过有人使用SimpleXML来做这件事,但我想知道是否有办法通过Zend框架 我想转换为数组的示例XML是: <library> <book> <authorFirst>Mark</authorFirst> <authorLast>Twain</authorLast> <title&

有没有一种方法可以使用Zend Framework和PHP将XML转换为数组?我见过有人使用SimpleXML来做这件事,但我想知道是否有办法通过Zend框架

我想转换为数组的示例XML是:

<library>
    <book>
        <authorFirst>Mark</authorFirst>
        <authorLast>Twain</authorLast>
        <title>The Innocents Abroad</title>
    </book>
    <book>
        <authorFirst>Charles</authorFirst>
        <authorLast>Dickens</authorLast>
        <title>Oliver Twist</title>
    </book>
</library>

如果您查看Zend_Config_XML的
部分,您将看到它们使用SimpleXML几乎完全做到了这一点。我猜呢?他们建议您也这样做。

ZF也使用SimpleXML。例如:

$xml = simplexml_load_string($data);
$books = array();
foreach ($xml->books as $book) {
    $books[] = (array)$book;
}
var\u dump($books)
将为您提供:

array(2) {
  [0]=>
  array(3) {
    ["authorFirst"]=>
    string(4) "Mark"
    ["authorLast"]=>
    string(5) "Twain"
    ["title"]=>
    string(20) "The Innocents Abroad"
  }
  [1]=>
  array(3) {
    ["authorFirst"]=>
    string(7) "Charles"
    ["authorLast"]=>
    string(7) "Dickens"
    ["title"]=>
    string(12) "Oliver Twist"
  }
}

如果你的书可能有嵌套元素,那么它会变得更复杂一些。

不幸的是,或者幸运的是,根据你的观点,Zend Framework没有一套适用于一切可能的类——到目前为止。您必须使用普通php(如simpleXML或DOMDocument)完成此任务,或者实现您自己的Zend类

将XML文件转换为PHP数组将适合Zend_过滤器。Zend_Filter现在转换了很多东西,还有一些东西正在开发中,但我看不到类似XML的东西

array(2) {
  [0]=>
  array(3) {
    ["authorFirst"]=>
    string(4) "Mark"
    ["authorLast"]=>
    string(5) "Twain"
    ["title"]=>
    string(20) "The Innocents Abroad"
  }
  [1]=>
  array(3) {
    ["authorFirst"]=>
    string(7) "Charles"
    ["authorLast"]=>
    string(7) "Dickens"
    ["title"]=>
    string(12) "Oliver Twist"
  }
}