Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Php » Convert XML into Associative Array in PHP

Convert XML Into Associative Array In PHP

Convert XML into Associative Array in PHP

Posted on August 12, 2021December 14, 2022 By XpertPhp

In this article, we will explain to you how to convert XML into associative array in PHP. so we will give you a simple example of an XML to array in PHP.

sometimes we need to convert XML file into an associative array in PHP. so here we use the file_get_contents() function to read an XML file into PHP. so you can see our following example.

Create XML File

In this step, we will create the XML data file and save the XML file as user.xml. so you can see the following XML code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0"?>
<user_details>
  <title>User Details</title>
  <totalUser>4</totalUser>
  <user_records>
<user>
  <firstname>john</firstname>
  <lastname>doe</lastname>
  <username>john123</username>
</user>
<user>
  <firstname>ajay</firstname>
  <lastname>devgan</lastname>
  <username>ajdev</username>
</user>
<user>
  <firstname>rohit</firstname>
  <lastname>sharma</lastname>
  <username>rs123</username>
</user>
<user>
  <firstname>kapil</firstname>
  <lastname>sharma</lastname>
  <username>ks123</username>
</user>
  </user_records>
</user_details>

Convert XML File Into String

in this step, we convert xml File to string using the file_get_contents() function in php.
Output

1
2
$xmlFile = "user.xml";
$xmlFileData = file_get_contents($xmlFile);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<user_details>
  <title>User Details</title>
  <totalUser>4</totalUser>
  <user_records>
<user>
  <firstname>john</firstname>
  <lastname>doe</lastname>
  <username>john123</username>
</user>
<user>
  <firstname>ajay</firstname>
  <lastname>devgan</lastname>
  <username>ajdev</username>
</user>
<user>
  <firstname>rohit</firstname>
  <lastname>sharma</lastname>
  <username>rs123</username>
</user>
<user>
  <firstname>kapil</firstname>
  <lastname>sharma</lastname>
  <username>ks123</username>
</user>
  </user_records>
</user_details>

Convert XML String to XML Object

Now, we convert xml string to xml object using the simplexml_load_string() function in php.

1
$xmlDataObject = simplexml_load_string($xmlFileData);
See also  PHP Ajax View Data in MySQL By Using Bootstrap Modal

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
SimpleXMLElement Object
(
    [title] => User Details
    [totalUser] => 4
    [user_records] => SimpleXMLElement Object
        (
            [user] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [firstname] => john
                            [lastname] => doe
                            [username] => john123
                        )
 
                    [1] => SimpleXMLElement Object
                        (
                            [firstname] => ajay
                            [lastname] => devgan
                            [username] => ajdev
                        )
 
                    [2] => SimpleXMLElement Object
                        (
                            [firstname] => rohit
                            [lastname] => sharma
                            [username] => rs123
                        )
 
                    [3] => SimpleXMLElement Object
                        (
                            [firstname] => kapil
                            [lastname] => sharma
                            [username] => ks123
                        )
 
                )
 
        )
 
)

Convert XML Object to json

we convert xml object to json string using the json_encode() function in php.

1
$jsonData = json_encode($xmlDataObject);

Output

1
{"title":"User Details","totalUser":"4","user_records":{"user":[{"firstname":"john","lastname":"doe","username":"john123"},{"firstname":"ajay","lastname":"devgan","username":"ajdev"},{"firstname":"rohit","lastname":"sharma","username":"rs123"},{"firstname":"kapil","lastname":"sharma","username":"ks123"}]}}

Convert json data to array

finally, we will decode JSON data to associative array using json_decode() function.

1
$associateArray = json_decode($jsonData, true);

Example

1
2
3
4
5
6
7
8
9
10
<?php
  $xmlFile = "user.xml";
  $xmlFileData = file_get_contents($xmlFile);
  $xmlDataObject = simplexml_load_string($xmlFileData);
  $jsonData = json_encode($xmlDataObject);
  $associateArray = json_decode($jsonData, true);
  echo "<pre>";
  print_r($associateArray);
  echo "</pre>"
?>

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Array
(
    [title] => User Details
    [totalUser] => 4
    [user_records] => Array
        (
            [user] => Array
                (
                    [0] => Array
                        (
                            [firstname] => john
                            [lastname] => doe
                            [username] => john123
                        )
 
                    [1] => Array
                        (
                            [firstname] => ajay
                            [lastname] => devgan
                            [username] => ajdev
                        )
 
                    [2] => Array
                        (
                            [firstname] => rohit
                            [lastname] => sharma
                            [username] => rs123
                        )
 
                    [3] => Array
                        (
                            [firstname] => kapil
                            [lastname] => sharma
                            [username] => ks123
                        )
 
                )
 
        )
 
)

Php

Post navigation

Previous Post: How to Remove file Extension from Filename in PHP
Next Post: Convert associative arrays into XML in PHP

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com