
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to convert an array to SimpleXML in PHP?
We can solved the above problem using array_walk_recursive() function.array_walk_recursive() is an inbuilt PHP function. This function converts array to XML document where keys of the array are converted into values and values of the array are converted into the element of XML.
Let' demonstrate with a simple example.
Example
<?php $array = array ( 'name' => 'alex', 'empdept' => 'account', 'address' => array ( 'city' => 'michigan' ), ); //This function create a xml object with element root. $xml = new SimpleXMLElement(''); array_walk_recursive($array, array ($xml,'addChild')); print $xml->asXML(); ?>
Output
<?xml version="1.0"?> <root> <name> alex </name> <empdept> account </empdept> <city> michigan </city > </root>
Note
If errors message display like PHP Fatal error: Uncaught Error: Class 'SimpleXMLElement' not found in then simply install php-xml, php-simplexml packages.
- Related Articles
- Convert object to an array in PHP.
- Convert an object to associative array in PHP
- How to convert PHP array to JavaScript array?
- How to convert array to string PHP?
- How to convert Multidimensional PHP array to JavaScript array?
- How to convert XML file into array in PHP?
- How to convert an object array to an integer array in Java?
- How to re-index an array in PHP?
- How to convert an array to string in java?
- How to Convert Hashtable into an Array?
- How to convert an object into an array in JavaScript?
- How to convert nested array pairs to objects in an array in JavaScript ?
- How to convert an object to byte array in java?
- How to convert an array to a list in Java?
- How to convert an Array to a Set in Java?

Advertisements