easy!

$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(array = json_decode($json,TRUE);
Answer from user1398287 on Stack Overflow
🌐
Wtools
wtools.io β€Ί convert-xml-to-php-array
Convert XML to PHP Array Online - wtools.io
Free tool for online converting XML into appropriate PHP type as Array.
🌐
Table Convert
tableconvert.com β€Ί home β€Ί convert xml to php array online
Convert XML to PHP Array Online - Table Convert
January 11, 2019 - The tool automatically parses XML structure and converts it to table format, supporting namespace, attribute handling, and complex nested structures. ... Edit data using our advanced online table editor with professional features. Supports deleting empty rows, removing duplicates, data transposition, sorting, regex find & replace, and real-time preview. All changes automatically convert to PHP format with precise, reliable results. ... Generate standard PHP array code that can be directly used in PHP projects, supporting associative and indexed array formats.
People also ask

How to use the Convert XML to PHP Array Online for free?
Upload your XML file, paste data, or extract from web pages using our free online table converter. Convert XML to PHP instantly with real-time preview and advanced editing. This XML to PHP converter lets you copy or download your PHP output right away.
🌐
tableconvert.com
tableconvert.com β€Ί home β€Ί convert xml to php array online
Convert XML to PHP Array Online - Table Convert
How to use the Convert XML to JSON Array Online for free?
Upload your XML file, paste data, or extract from web pages using our free online table converter. Convert XML to JSON instantly with real-time preview and advanced editing. This XML to JSON converter lets you copy or download your JSON output right away.
🌐
tableconvert.com
tableconvert.com β€Ί home β€Ί convert xml to json array online
Convert XML to JSON Array Online - Table Convert
What is PHP Array format?
PHP is a popular server-side scripting language, with arrays being its core data structure, widely used in web development and data processing.
🌐
tableconvert.com
tableconvert.com β€Ί home β€Ί convert xml to php array online
Convert XML to PHP Array Online - Table Convert
🌐
Functions-Online
functions-online.com β€Ί xml2Array.html
xml2Array - functions-online
Test and run xml2Array online in your browser. The function recursivley converts a xml string into an array.
🌐
Packagist
packagist.org β€Ί packages β€Ί mtownsend β€Ί xml-to-array
mtownsend/xml-to-array - Packagist.org
$xml = <<<XML <?xml version="1.0"?> <request> <carrier>fedex</carrier> <id>123</id> <tracking_number>9205590164917312751089</tracking_number> </request> XML; $array = xml_to_array($xml); // $array is: [ 'carrier' => 'fedex', 'id' => '123', 'tracking_number' => '9205590164917312751089' ];
🌐
SmallDev
smalldev.tools β€Ί xml-decoder-online
XML to array converter - SmallDev.tools
πŸ‘Convert any XML format to array format instantly. Fast, simple & secure. One click to copy & share output.
🌐
Table Convert
tableconvert.com β€Ί home β€Ί convert xml to json array online
Convert XML to JSON Array Online - Table Convert
January 11, 2019 - Generate multiple JSON format outputs: standard object arrays, 2D arrays, column arrays, and key-value pair formats. Supports beautified output, compression mode, custom root object names, and indentation settings, perfectly adapting to various API interfaces and data storage needs. Extract tables from any website with one click. Convert to 30+ formats including Excel, CSV, JSON instantly - no copy-pasting required. Converting XML to JSON?
Find elsewhere
🌐
PHP
php.net β€Ί manual β€Ί en β€Ί function.xml-parse-into-struct.php
PHP: xml_parse_into_struct - Manual
<?xml version="1.0"?> <moldb> <molecule> <name>Alanine</name> <symbol>ala</symbol> <code>A</code> <type>hydrophobic</type> </molecule> <molecule> <name>Lysine</name> <symbol>lys</symbol> <code>K</code> <type>charged</type> </molecule> </moldb> And some code to parse the document and generate the appropriate objects: Example #3 parsemoldb.php - parses moldb.xml into an array of molecular objects
🌐
GitHub
github.com β€Ί mtownsend5512 β€Ί xml-to-array
GitHub - mtownsend5512/xml-to-array: Easily convert valid xml to a php array.
$xml = <<<XML <?xml version="1.0"?> <request> <carrier>fedex</carrier> <id>123</id> <tracking_number>9205590164917312751089</tracking_number> </request> XML; $array = xml_to_array($xml); // $array is: [ 'carrier' => 'fedex', 'id' => '123', 'tracking_number' => '9205590164917312751089' ];
Starred by 117 users
Forked by 14 users
Languages Β  PHP 100.0% | PHP 100.0%
🌐
Packagist
packagist.org β€Ί packages β€Ί dmb β€Ί xml-converter
dmb/xml-converter - Packagist
A package that allows you to easily convert your XML string into array format or generate an XML string from an array.
🌐
GitHub
github.com β€Ί vyuldashev β€Ί xml-to-array
GitHub - vyuldashev/xml-to-array: A simple class to convert an xml to array
use Vyuldashev\XmlToArray\XmlToArray; $xml = '<items> <good_guy> <name>Luke Skywalker</name> <weapon>Lightsaber</weapon> </good_guy> <bad_guy> <name>Sauron</name> <weapon>Evil Eye</weapon> </bad_guy> </items>'; $result = XmlToArray::convert($xml); After running this piece of code $result will contain: array:1 [ "items" => array:2 [ "good_guy" => array:2 [ "name" => "Luke Skywalker" "weapon" => "Lightsaber" ] "bad_guy" => array:2 [ "name" => "Sauron" "weapon" => "Evil Eye" ] ] ]
Starred by 36 users
Forked by 15 users
Languages Β  PHP 100.0% | PHP 100.0%
🌐
Wtools
wtools.io β€Ί convert-php-array-to-xml
Convert PHP Array to XML Online - wtools.io
Convert XML to PHP Array OnlineConvert PHP Array to JSONConvert PHP Array to YAMLSerialize PHP arrayConvert PHP Array to PListConvert PHP array to JS ObjectConvert print_r output to PHP array
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί php β€Ί how-to-convert-xml-file-into-array-in-php
How to convert XML file into array in PHP? - GeeksforGeeks
July 11, 2025 - json_decode() function: The json_decode() function is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. Step 1: Creating an XML file (Optional): Create an XML file which need to convert into the array.
🌐
Table Convert
tableconvert.com β€Ί home β€Ί convert xml to asp array online
Convert XML to ASP Array Online - Table Convert
January 11, 2019 - The tool automatically parses XML structure and converts it to table format, supporting namespace, attribute handling, and complex nested structures. ... Edit data using our advanced online table editor with professional features. Supports deleting empty rows, removing duplicates, data transposition, sorting, regex find & replace, and real-time preview. All changes automatically convert to ASP format with precise, reliable results. ... Generate ASP-compatible array code with support for VBScript and JScript syntax, usable in ASP.NET projects.
Top answer
1 of 2
3

You can json_encode() then json_decode() as array and use simplexml_load_string()

Steps:

1) First convert your XML into readable string object using simplexml_load_string().

2) Then json_encode() it.

3) json_decode() it, with second parameter TRUE, which will return array instead of object.

4) Now, your XML is converted into an array.

5) Take a blank array, loop over array from above code and append elements to it.

To get desired output:

<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>

<course category = "JAVA">
  <title lang = "en">Java</title>
  <tutor>Gopal</tutor>
  <duration>3</duration>
  <price>$30</price>
</course>

<course category = "HADOOP">
  <title lang = "en">Hadoop</title>.
  <tutor>Satish</tutor>
  <duration>3</duration>
  <price>$50</price>
</course>

<course category = "HTML">
  <title lang = "en">html</title>
  <tutor>raju</tutor>
  <duration>5</duration>
  <price>$50</price>
</course>

<course category = "WEB">
  <title lang = "en">Web Technologies</title>
  <tutor>Javed</tutor>
  <duration>10</duration>
  <price>$60</price>
</course>
</tutorialspoint>';

$arr = [];
$array = json_decode(json_encode(simplexml_load_string($xml)),true);
if ( ! empty($array)) {
 $i=0;
 foreach ($array['course'] as $elem) {
   $arr[$i]['title'] = $elem['title'];
   $arr[$i]['tutor'] = $elem['tutor'];
   $arr[$i]['duration'] = $elem['duration'];
   $arr[$i]['price'] = $elem['price'];
  ++$i;
 }
}
echo '<pre>';print_r($arr);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [title] => Java
            [tutor] => Gopal
            [duration] => 3
            [price] => $30
        )

    [1] => Array
        (
            [title] => Hadoop
            [tutor] => Satish
            [duration] => 3
            [price] => $50
        )

    [2] => Array
        (
            [title] => html
            [tutor] => raju
            [duration] => 5
            [price] => $50
        )

    [3] => Array
        (
            [title] => Web Technologies
            [tutor] => Javed
            [duration] => 10
            [price] => $60
        )

)

Working Code:

2 of 2
1

This code snippet will convert your XML to array

$array = json_decode(json_encode((array)simplexml_load_string($xml)),true);
echo '<pre>';
print_r($array);
🌐
Utilities and Tools
utilities-online.info β€Ί xmltojson
XML to JSON & JSON to XML converter online
If you enter the code in the XML input box, the converted JSON file will be: The below rules will be applied while performing the XML conversion: The attributes of the code will be treated as regular JSON properties. The sequence of two or more similar elements will be translated to a JSON array.
🌐
GitHub
github.com β€Ί Sammyjo20 β€Ί xml-to-array
GitHub - Sammyjo20/xml-to-array: πŸ”€ Convert an XML string into an easy to understand array
December 2, 2023 - <?php use Sammyjo20\XmlToArray\XmlToArray; $xml = '<items> <good_guy> <name>Luke Skywalker</name> <weapon>Lightsaber</weapon> </good_guy> <bad_guy> <name>Sauron</name> <weapon>Evil Eye</weapon> </bad_guy> </items>'; $result = XmlToArray::convert($xml); array:1 [ "items" => array:2 [ "good_guy" => array:2 [ "name" => "Luke Skywalker" "weapon" => "Lightsaber" ] "bad_guy" => array:2 [ "name" => "Sauron" "weapon" => "Evil Eye" ] ] ] This package was originally created by vyuldashev. This package is an up-to-date version with some bugs fixed.
Author Β  Sammyjo20