easy!
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(
array = json_decode($json,TRUE);
Answer from user1398287 on Stack Overfloweasy!
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(
array = json_decode($json,TRUE);
Answer from user1398287 on Stack Overflow Top answer 1 of 12
538
easy!
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode(
array = json_decode($json,TRUE);
2 of 12
145
Another option is the SimpleXML extension (I believe it comes standard with most php installs.)
http://php.net/manual/en/book.simplexml.php
The syntax looks something like this for your example
$xml = new SimpleXMLElement($xmlString);
echo $xml->bbb->cccc->dddd['Id'];
echo $xml->bbb->cccc->eeee['name'];
// or...........
foreach ($xml->bbb->cccc as $element) {
foreach($element as
val) {
echo "{$key}: {$val}";
}
}
PHP
php.net › manual › en › book.simplexml.php
PHP: SimpleXML - Manual
For example, this does not work: $xml = new SimpleXMLElement($page); $country = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode; $_SESSION['country'] = $country; This will work: $_SESSION['country'] = (string) $country; ... Here ...
Videos
07:17
How To Read XML Data To Array in PHP Tutorial (Article Demo #6) ...
01:51
How to Convert XML to Array in PHP Easily - YouTube
📚 Master XML Arrays in PHP! Ever wondered how to turn XML ...
How to Convert Array to XML in PHP? | PHP Array to XML ...
11:28
How To Convert Associative Arrays into XML in PHP |How To Convert ...
18:26
Convert Array to XML and XML to Array in PHP - YouTube
Envato Tuts+
code.tutsplus.com › parse-xml-to-an-array-in-php-with-simplexml--cms-35529t
Parse XML to an Array in PHP With SimpleXML | Envato Tuts+
In this post, you'll learn how to parse XML into an array in PHP.
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' ]; ... ...
GitHub
github.com › clearstream › xml-to-array
GitHub - clearstream/xml-to-array: Incredibly simple XML to array PHP converter
use Clearstream\XmlToArray\XmlToArray; ... <cdata><![CDATA[<sender>John Doe</sender>]]></cdata> </response> XML; $array = XmlToArray::convert($xml);...
Author clearstream
GitHub
github.com › spatie › array-to-xml
GitHub - spatie/array-to-xml: A simple class to convert an array to xml · GitHub
The string to be rendered as the XML tag. ... A unique string that avoids overwriting of duplicate keys in PHP arrays.
Starred by 1.2K users
Forked by 214 users
Languages PHP
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', ...
Starred by 117 users
Forked by 14 users
Languages PHP 100.0% | PHP 100.0%
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php xml into array
PHP XML into Array | Working of the PHP XML Into Array
July 3, 2023 - Finally, use the json_decode() function to decode the json string, which converts the json encoded string into an array. Example to load an XML file and convert it into a PHP array –
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
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%
Envato Tuts+
code.tutsplus.com › tutorials › parse-xml-to-an-array-in-php-with-simplexml--cms-35529
Parse XML to an Array in PHP With SimpleXML - Code
December 8, 2020 - In this post, you'll learn how to parse XML into an array in PHP.
TutorialsPoint
tutorialspoint.com › how-to-convert-xml-file-into-array-in-php
How to convert XML file into array in PHP?
June 29, 2020 - After the above step, we can use json_encode() function to present the object in the json encoded string. The json_decode() function decode a JSON string. It converts a JSON encoded string into a PHP array. <?php // xml file path $path = "abc.xml"; $xmlfile = file_get_contents($path); $new ...
LIVEcommunity
live.paloaltonetworks.com › t5 › automation-api-discussions › php-xml-to-array › td-p › 20185
LIVEcommunity - PHP - XML to Array - LIVEcommunity - 20185
April 3, 2015 - So I've been working with the API pretty frequently and have run into a bit of a snag that has caught me now a few times. I'm using CURL with PHP to interact and whenever I get a response, I do a conversion as such: $xmlarr = json_decode(json_encode((array)simplexml_load_string($result)),1); This...
W3Schools
w3schools.com › php › func_xml_parse_into_struct.asp
PHP xml_parse_into_struct() Function
Index array - containing pointers to the location of the values in the Value array · xml_parse_into_struct(parser, data, values, index) ❮ PHP XML Parser Reference · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS ·
Linux Hint
linuxhint.com › xml_to_associative_array_in_php
Convert XML to Associative Array in PHP
November 7, 2020 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Adsar
adsar.co.uk › xml-to-array-php
XML to Arrays in PHP - adsar
function xml2array($xml, $flattenValues=true, $flattenAttributes = true, $flattenChildren=true, $valueKey='@value', $attributesKey='@attributes', $childrenKey='@children') { $return = array(); if (!($xml instanceof SimpleXMLElement)) { return $return; } $name = $xml->getName(); $_value = trim((string) $xml); if (strlen($_value) == 0) { $_value = null; }; if ($_value !== null) { if (!$flattenValues) { $return[$valueKey] = $_value; } else { $return = $_value; } } $children = array(); $first = true; foreach ($xml->children() as $elementName => $child) { $value = xml2array($child, $flattenValues,
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.
Laracasts
laracasts.com › discuss › channels › laravel › converting-array-to-xml
converting array to xml
We cannot provide a description for this page right now
Top answer 1 of 4
20
This is your best bet, and it should eliminate all the SimpleXMLElement objects and instead give you nothing but arrays:
$xml = simplexml_load_file("test.xml");
$xml_array = unserialize(serialize(json_decode(json_encode((array) $xml), 1)));
print_r($xml_array);
Makes the difference between this:

And this:

Hope that helps... :)
2 of 4
9
You can't access children by using a foreach on the node itself, you need to use .children():
$s =<<<EOS
<root>
<Formula>
<formulaname>Basic</formulaname>
<movespeed>1</movespeed>
<box>4</box>
<chicken>3</chicken>
<ducks>1</ducks>
<cereal>2</cereal>
</Formula>
</root>
EOS;
$xml = simplexml_load_string($s);
foreach ($xml->Formula as $element) {
foreach($element->children() as $key => $val) {
echo "{$key}: {$val}";
}
}