【问题标题】:How to Convert XML file with attributes to an array如何将具有属性的 XML 文件转换为数组
【发布时间】:2013-03-21 18:06:00
【问题描述】:

我尝试将一个简单的 xml 文件转换为一个数组,但转换不会将“id”属性保留为键数组。 XML 来源:

<mapi>
    <categoriesList>
        <category id="310">Autres</category>
        <category id="574">Bière</category>
        <category id="495">Biscuits</category>
        <category id="444">Bonbons</category>
        <category id="435">Champagne</category>
        <category id="371">Cidre</category>
        <category id="215">Condiments</category>
        <category id="8">Fruits</category>
        <category id="445">Poissons</category>
        <category id="578">Produits laitiers</category>
        <category id="539">Spiritueux</category>
        <category id="259">Viandes</category>
        <category id="126">Vin</category>
    </categoriesList>
</mapi>

我喜欢这样一个简单的数组:

array (
    [310] => Autres
    [574] => Bière
    [495] => Biscuits
    [444] => Bonbons 
    [435] => Champagne
    [371] => Cidre
    [215] => Condiments
    [8] => Fruits 
    [445] => Poissons
    [578] => Produits laitiers
    [539] => Spiritueux
    [259] => Viandes
    [126] => Vin
)

感谢您的帮助 迪米特里

【问题讨论】:

  • 你的代码在哪里?你尝试了什么?

标签: php xml arrays


【解决方案1】:

这样的事情应该可以工作:

function XMLtoArray($xml) {
    $xmlArray = array();
    $dom = new DOMDocument;
    $dom->load($xml);
    $categories = $dom->getElementsByTagName("category");
    foreach($categories as $category) {
        $id = $category->getAttribute("id");
        $xmlArray[$id] = $category->nodeValue;
    }
    return($xmlArray);
}

然后像这样调用函数:

$myArray = XMLtoArray("path/to/file.xml");

【讨论】:

  • 完美运行:-)。谢谢。
  • 没问题;如果您愿意,请随意选择此作为答案。
【解决方案2】:
<?php
    include ("htmlparser.php");
    $string = '
<mapi>
    <categoriesList>
        <category id="310">Autres</category>
        <category id="574">Bière</category>
        <category id="495">Biscuits</category>
        <category id="444">Bonbons</category>
        <category id="435">Champagne</category>
        <category id="371">Cidre</category>
        <category id="215">Condiments</category>
        <category id="8">Fruits</category>
        <category id="445">Poissons</category>
        <category id="578">Produits laitiers</category>
        <category id="539">Spiritueux</category>
        <category id="259">Viandes</category>
        <category id="126">Vin</category>
    </categoriesList>
</mapi>
';
 $html = str_get_html($string);
  foreach($html->find('category') as $element){
    $array[] = $element -> innertext ;
  }

  echo '<pre>';
  print_r($array);
?>

你需要下载这个库:http://simplehtmldom.sourceforge.net/manual.htm#section_dump

【讨论】:

    【解决方案3】:

    这是一个非常简单的任务,SimpleXMLElement

    $sxe = simplexml_load_string($xml);
    $asarray = array();
    foreach ($sxe->categoriesList->category as $c) {
        $asarray[(int) $c['id']] = (string) $c;
    }
    
    var_export($asarray);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      相关资源
      最近更新 更多