Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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
需要将xml转换为php数组吗_Php_Xml - Fatal编程技术网

需要将xml转换为php数组吗

需要将xml转换为php数组吗,php,xml,Php,Xml,我使用的XML类似于: [domain] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => AlOkJainist [com] => y [comscore] =

我使用的XML类似于:

[domain] => Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [name] => AlOkJainist
                    [com] => y
                    [comscore] => 805
                    [net] => y
                    [netscore] => 779
                    [tv] => y
                    [tvscore] => 753
                    [cc] => y
                    [ccscore] => 728
                )

        )

    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [name] => BargainsdiAlOg
                    [com] => y
                    [comscore] => 805
                    [net] => y
                    [netscore] => 779
                    [tv] => y
                    [tvscore] => 753
                    [cc] => y
                    [ccscore] => 728
                )

        )

    [2] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [name] => AlOkayJain
                    [com] => y
                    [comscore] => 792
                    [net] => y
                    [netscore] => 766
                    [tv] => y
                    [tvscore] => 740
                    [cc] => y
                    [ccscore] => 715
                )

        )

    )

)
我想创建PHP数组,如下所示:

array(
    'AlOkJainist' => array([com] => y, [net] => y),
    'BargainsdiAlOg' => array([com] => y, [net] => y),
    'AlOkayJain' => array([com] => y, [net] => y),

);
请帮助我,我已经尝试过了,但没有成功。

您可以使用php提供的库。 传递xml文档,然后重新循环并构建一个数组

function toArray( $obj, &$arr = null ) {
    if ( is_null( $arr ) )   $arr = array();
    if ( is_string( $obj ) ) $obj = new SimpleXMLElement( $obj );

    // Get attributes for current node and add to current array element
    $attributes = $obj->attributes();
    foreach ($attributes as $attrib => $value) {
      $arr['attributes'][$attrib] = (string)$value;
    }

    $children = $obj->children();
    $executed = false;
    // Check all children of node
    foreach ($children as $elementName => $node) {
      // Check if there are multiple node with the same key and generate a multiarray
      if($arr[$elementName] != null) {
        if($arr[$elementName][0] !== null) {
          $i = count($arr[$elementName]);
          toArray($node, $arr[$elementName][$i]);
        } else {
          $tmp = $arr[$elementName];
          $arr[$elementName] = array();
          $arr[$elementName][0] = $tmp;
          $i = count($arr[$elementName]);
          toArray($node, $arr[$elementName][$i]);
        }
      } else {
        $arr[$elementName] = array();
        toArray($node, $arr[$elementName]);
      }
      $executed = true;
    }
    // Check if is already processed and if already contains attributes
    if(!$executed && $children->getName() == "" && !isset ($arr['attributes'])) {
      $arr = (String)$obj;
    }
    return $arr;
  }