2

I try to convert a simple xml file to an array but the conversion do not keep the "id" attributes as the key array. XML source:

<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>

Id like a simple array like this:

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
)

Thank your for your help Dimitri

1
  • Where's your code? What did you try? Commented Mar 21, 2013 at 18:07

3 Answers 3

1

Something like this should work:

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);
}

Then call the function like so:

$myArray = XMLtoArray("path/to/file.xml");
Sign up to request clarification or add additional context in comments.

1 Comment

Not a problem; feel free to choose this as the answer if you would like.
0
<?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);
?>

You need to download this library: http://simplehtmldom.sourceforge.net/manual.htm#section_dump

Comments

0

This is a very simple task with SimpleXMLElement:

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

var_export($asarray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.