php xml to array - Web Development and Web Design Codes

Latest

Saturday, April 21, 2018

php xml to array

php xml to array
php xml to array
php xml to array
Hi Friends in this lesson we are going to learn how to convert xml file into array using php..
this is very simple codes to convert php xml to array..

create xml file: file.xml
<?xml version="1.0"?>
<user_info>
  <title>Users Information</title>
  <totalRows>3</totalRows>
  <rows>
    <user>
      <name>ANJAN KUMAR</name>
      <email>Anjankumardhali@gmail.com</email>
	  <education>Physics</education>
    </user>
    <user>
      <name>Palash Mondal</name>
      <email>Palsh@mail.com</email>
	  <education>Computer Science</education>
    </user>
    <user>
      <name>Nikita Adikary</name>
      <email>Niki@nikita.com</email>
	  <education>Software Engineering</education>
    </user>
  </rows>
</user_info>

Create php file to access xml file: xml_to_array.php
<?php
//xml file path
$path = "file.xml";
//read entire file into string
$xmlfile = file_get_contents($path);
//convert xml string into an object
$xml = simplexml_load_string($xmlfile);
$json  = json_encode($xml);
//convert into associative array
$xmlArr = json_decode($json, true);
print_r($xmlArr);
?>

That's it friends how to convert xml to array using php..if you like this post please share with your friends..Thank You..

No comments:

Post a Comment

Thank You for Your Comment