How To Convert XML To Associative Array in PHP

How To Convert XML To Associative Array in PHP

Today, We want to share with you How To Convert XML To Associative Array in PHP.In this post we will show you Parse XML into associative array using PHP, hear for Convert a SimpleXML object to associative array we will give you demo and example for implement.In this post, we will learn about Converting an XML Object to Associative Array in PHP with an example.

How To Convert XML To Associative Array in PHP

There are the Following The simple About How To Convert XML To Associative Array in PHP Full Information With Example and source code.

As I will cover this Post with live Working example to develop Convert Array to XML and XML to Array in PHP, so the XML into Associative Array using PHP for this example is following below.

Read Also:  delete Query In Codeigniter Example Tutorial

Step 1: Sample XML file

<?xml version='1.0'?>  
<moleculedb>  
    <ptitle name='Mobile'>  
        <tag>elc</tag>  
        <qty>15</qty>  
    </ptitle>  
    <ptitle name='Book'>  
        <tag>school</tag>  
        <qty>20</qty>  
    </ptitle>
	<ptitle name='Assertive'>  
        <tag>company</tag>  
        <qty>10</qty>  
    </ptitle>
</moleculedb>

Step 2: PHP Convert sample.xml File Into String

$product_xmlfile = file_get_contents($path);

Step 3: Convert string of XML into an Object

$product_obj= simplexml_load_string($product_xmlfile);

Step 4: PHP Encode XML Object Into JSON

$json  = json_encode($product_obj);

Step 5: PHP Decode Json Object

$results_data = json_decode($json, true);

PHP Full Source Code

$product_xmlfile = file_get_contents($path);
$product_obj= simplexml_load_string($product_xmlfile);
$json  = json_encode($product_obj);
$results_data = json_decode($json, true);

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Update Query In Codeigniter Example Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How To Convert XML To Associative Array in PHP.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *