Search code examples
phparraysxmlsimplexmlxmldom

Convert XML to Multidimensional Array


I have xml file like this:

<?xml version="1.0"?>
<table>
<form>
    <input name="id">1</input>
    <input name="name">Steven Law</input>
    <input name="position">Sales Manager</input>
</form>
<form>
    <input name="id">2</input>
    <input name="name">Martin Wow</input>
    <input name="position">Production</input>
</form>
<form>
    <input name="id">3</input>
    <input name="name">Mike Pan</input>
    <input name="position">Office boy</input>
</form>
</table>

I tried to get element value.I don't know how to get element value for each field. So far I'm try like this, it is just get an attribute value:

<?php
$dom = new DOMDocument();
$dom->load("data.xml");

$input = $dom->getElementsByTagName('input');
foreach($input as $inputs){
  $name= $inputs->getAttribute('name');
  //$value = $field->name('id');
  echo "$name =<br>";
}

OUTPUT

id =
name =
position =
id =
name =
position =
id =
name =
position =

What I want is will be output id=1 name=Steven Law ... id=3 name=mike pan position=office boy. After that, from output I can make to an array.

Oh one more, please tell me about different XMLDOM and SimpleXML. I don't know must choose what. Please help me


Solution

  • Reading the value can be done with DOMElement::nodeValue:

    <?php
    $dom = new DOMDocument();
    $dom->load("data.xml");
    
    $input = $dom->getElementsByTagName('input');
    foreach($input as $inputs){
      $name= $inputs->getAttribute('name');
    
      $value = $inputs->nodeValue; //reading the value
      echo "$name = $value<br>"; 
    }
    

    Notice

    To test this, the XML example has to be corrected: In the first block the <input> elements are closed with </field>. This has to be changed to </input>.

    You asked for an array. It depends on how you need the data in your application. Here are two possibilities, which should give you an idea to go on:

    a) Creating an array (the easy way)

    <?php
    $dom = new DOMDocument();
    $dom->load("data.xml");
    
    $array=array(); //init
    
    $input = $dom->getElementsByTagName('input');
    foreach($input as $inputs){
      $name= $inputs->getAttribute('name');
      $value = $inputs->nodeValue;
    
      //adding a value
      $array[]=array(
        'name' => $name,
        'value' => $value,
        );
    }
    // as an example, just print it
    print_r($array);
    

    You'll get something like this:

    Array
    (
        [0] => Array
            (
                [name] => id
                [value] => 1
            )
    
        [1] => Array
            (
                [name] => name
                [value] => Steven Law
            )
    
        [2] => Array
            (
                [name] => position
                [value] => Sales Manager
            )
    
        [3] => Array
            (
                [name] => id
                [value] => 2
            )
    
        [4] => Array
            (
                [name] => name
                [value] => Martin Wow
            )
    
        [5] => Array
            (
                [name] => position
                [value] => Production
            )
    
        [6] => Array
            (
                [name] => id
                [value] => 3
            )
    
        [7] => Array
            (
                [name] => name
                [value] => Mike Pan
            )
    
        [8] => Array
            (
                [name] => position
                [value] => Office boy
            )
    )
    

    b) Creating an array (more convenient to use later on)

    <?php
    $dom = new DOMDocument();
    $dom->load("data.xml");
    
    //init
    $array=array();
    //get all form tags
    $forms=$dom->getElementsByTagName('form');
    foreach($forms as $form) {
        //get all input-Tags from this form
        $input = $form->getElementsByTagName('input');
    
        //create an empty element
        $element=array();
        //walk through the input elements of the current form element
        foreach($input as $inputs){
          $name= $inputs->getAttribute('name');
          $value = $inputs->nodeValue;
          //add the data to the element
          $element[$name]=$value;
        }
        //add the element to your array
        $array[]=$element;
    }
    // as an example, just print it
    print_r($array);
    

    Here you'll walk through your <form> elements first. Then, in every form you read the <input> elements. So, you'll get an entry for every form block containing the inputs:

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Steven Law
                [position] => Sales Manager
            )
    
        [1] => Array
            (
                [id] => 2
                [name] => Martin Wow
                [position] => Production
            )
    
        [2] => Array
            (
                [id] => 3
                [name] => Mike Pan
                [position] => Office boy
            )
    
    )