How to Convert XML File to Array in PHP?
In this quick example, let's see how to convert xml file to array in php. In this article, we will implement a how to convert xml to array in php. We will use how to convert xml data to array in php. i would like to share with you php convert xml to array example. Let's see bellow example php xml to array example.
In this example, i will give you very simple example of convert xml to array in php. so here, we will keep one simple example file and convert into php array.
Let's see now:
Add Simple Xml File:
you can create or download simple xml file as like bellow:
PHP Code:
now you can see how convert xml to array using bellow code:
<?php
$xmlString = file_get_contents('sample.xml');
$xmlObject = simplexml_load_string($xmlString);
$json = json_encode($xmlObject);
$phpArray = json_decode($json, true);
print_r($phpArray);
?>
you will see bellow output:
Output:
Array
(
[food] => Array
(
[0] => Array
(
[name] => Belgian Waffles
[price] => $5.95
[description] => Two of our famous Belgian Waffles with plenty of real maple syrup
[calories] => 650
)
[1] => Array
(
[name] => Strawberry Belgian Waffles
[price] => $7.95
[description] => Light Belgian waffles covered with strawberries and whipped cream
[calories] => 900
)
[2] => Array
(
[name] => Berry-Berry Belgian Waffles
[price] => $8.95
[description] => Light Belgian waffles covered with an assortment of fresh berries and whipped cream
[calories] => 900
)
[3] => Array
(
[name] => French Toast
[price] => $4.50
[description] => Thick slices made from our homemade sourdough bread
[calories] => 600
)
[4] => Array
(
[name] => Homestyle Breakfast
[price] => $6.95
[description] => Two eggs, bacon or sausage, toast, and our ever-popular hash browns
[calories] => 950
)
)
)
you can check with this code.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Read XML File in Laravel?
- PHP Move File from One Folder to Another Example
- PHP Laravel Left Join Query Example
- PHP Remove Duplicates from Multidimensional Array Example
- PHP CKEditor with Image Upload using KCFinder Example
- How to Check File is Exists or Not in PHP?
- How to Check If String is URL or Not in PHP?
- How to Convert Array Key to Lowercase in PHP?
- How to Convert Array Values to Lowercase in PHP?
- How to Import CSV File using MySQL?
- How to Get Product using Rakuten Marketing API in PHP?
- How to Find Day Name from Specific Date in PHP?