Tagged: php associative array to xml

Convert Array to XML and XML to Array using PHP 0

Convert Array to XML and XML to Array using PHP

Convert Array to XML and XML to Array using PHP

In this Post We Will Explain About is Convert Array to XML and XML to Array using PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Convert PHP associative array to XML Example

In this post we will show you Best way to implement How To Convert XML To Associative Array in PHP, hear for Array to Xml Conversion and Xml to Array Convert in PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Parsing XMLdata and converting in array using PHP

The makeDataXML() methods converts simple example of the PHP multidimensional array to XML data file. and then The All the data array needs to be simple all the data passed as a parameter in makeDataXML() methods. The makeDataXML() methods make an XML data document using PHO DOMDocument class and insert the simple PHP array data content in this XML data document. At the simple end, the data XML document(DOM) is saved data as an Data XML file using PHP in the specified root file path.

Convert simple PHP Multidimensional Array to XML data File

makeDataXML() methods:

function makeDataXML($data) {
    $title = $data['title'];
    $rowCount = count($data['students']);
    
    //create the xml document
    $xmlDoc = new DOMDocument();
    
    $root = $xmlDoc->appendChild($xmlDoc->createElement("student_info"));
    $root->appendChild($xmlDoc->createElement("title",$title));
    $root->appendChild($xmlDoc->createElement("totalRows",$rowCount));
    $tabUsers = $root->appendChild($xmlDoc->createElement('rows'));
    
    foreach($data['students'] as $student){
        if(!empty($student)){
            $tabUser = $tabUsers->appendChild($xmlDoc->createElement('student'));
            foreach($student as $key=>$val){
                $tabUser->appendChild($xmlDoc->createElement($key, $val));
            }
        }
    }
    
    header("Content-Type: text/plain");
    
    //make the output pretty
    $xmlDoc->formatOutput = true;
    
    //save xml file
    $file_name = str_replace(' ', '_',$title).'_'.time().'.xml';
    $xmlDoc->save("files/" . $file_name);
    
    //return xml file name
    return $file_name;
}

PHP Example of the Multidimensional Array

$data = array(
    'title' => 'Students Information',
    'students' => array(
        array('name' => 'jaydeep Gondaliya', 'email' => '[email protected]'),
        array('name' => 'mayur dhameliya', 'email' => '[email protected]'),
        array('name' => 'Hitesh Dhameliya', 'email' => '[email protected]')
    )
);

PHP simple Array to XML File convert

echo makeDataXML($data);

The example code will create the following XML document.

<?xml version="1.0"?>
<student_info>
  <title>Students Information</title>
  <totalRows>3</totalRows>
  <rows>
    <student>
      <name>jaydeep Gondaliya</name>
      <email>[email protected]</email>
    </student>
    <student>
      <name>mayur dhameliya</name>
      <email>[email protected]</email>
    </student>
    <student>
      <name>Hitesh Dhameliya</name>
      <email>[email protected]</email>
    </student>
  </rows>
</student_info>

Convert XML to PHP Associative Array

//here simple xml file path using php
$datapath = "files/path-to-document.xml";

//read data simple entire file into string using file_get_contents
$xml_getfile = file_get_contents($datapath);

//convert simple Data xml data string into an object
$data_xml = simplexml_load_string($xml_getfile);

//convert data simple into json
$json  = json_encode($data_xml);

//convert data into associative array using PHP
$xmlArr = json_decode($json, true);

print_r($xmlArr);

Output

The best example source code will simple convert the XML file using PHP to the following simple source code to associative array in php.

Array
(
    [title] => Students Information
    [totalRows] => 3
    [rows] => Array
        (
            [student] => Array
                (
                    [0] => Array
                        (
                            [name] => jaydeep Gondaliya
                            [email] => [email protected]
                        )

                    [1] => Array
                        (
                            [name] => mayur dhameliya
                            [email] => [email protected]
                        )

                    [2] => Array
                        (
                            [name] => Hitesh Dhameliya
                            [email] => [email protected]
                        )

                )

        )

)

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is php – How to convert array to SimpleXML And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.