How to convert XML file into array in PHP? Last Updated : 02 Jul, 2019 Comments Improve Suggest changes Like Article Like Report Given an XML document and the task is to convert an XML file into PHP array. To convert the XML document into PHP array, some PHP functions are used which are listed below: file_get_contents() function: The file_get_contents() function is used to read a file as string. This function uses memory mapping techniques which are supported by the server and thus enhances the performance by making it a preferred way of reading contents of a file. simplexml_load_string() function: Sometimes there is a need of parsing XML data in PHP. There are a handful of methods available to parse XML data. SimpleXML is one of them. Parsing an XML document means that navigating through the XML document and return the relevant pieces of information. Nowadays, a few APIs return data in JSON format but there are still a large number of websites which returns data in XML format. So we have to master in parsing an XML document if we want to feast on APIs available. json_encode() function: The json_encode() function is used to encode a JSON string and return the JSON representation of value. 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. GFG.xml xml <?xml version='1.0'?> <firstnamedb> <firstname name='Akshat'> <symbol>AK</symbol> <code>A</code> </firstname> <firstname name='Sanjay'> <symbol>SA</symbol> <code>B</code> </firstname> <firstname name='Parvez'> <symbol>PA</symbol> <code>C</code> </firstname> </firstnamedb> Step 2: Convert the file into string: XML file will import into PHP using file_get_contents() function which read the entire file as a string and store into a variable. Step 3: Convert the string into an Object: Convert the string into an object which can be easily done through some inbuilt functions simplexml_load_string() of PHP. Step 4: Convert the object into JSON: The json_encode() function is used to encode a JSON string and return the JSON representation of value. Step 5: Decoding the JSON Object: The json_decode() function decode a JSON string. It converts a JSON encoded string into a PHP variable. Example: php <?php // xml file path $path = "GFG.xml"; // Read entire file into string $xmlfile = file_get_contents($path); // Convert xml string into an object $new = simplexml_load_string($xmlfile); // Convert into json $con = json_encode($new); // Convert into associative array $newArr = json_decode($con, true); print_r($newArr); ?> Output: Comment More infoAdvertise with us Next Article How to convert XML file into array in PHP? S ShubhamDodeja Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Convert File Content to Byte Array in PHP ? Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility t 5 min read How to Convert Array to String in PHP? In PHP, Arrays are used to store multiple values in a single variable. However, sometimes we need to convert these arrays into strings:To display array data in a readable format.To join array items into a single sentence or list.To use the array data where only strings are accepted.To prepare data f 2 min read How to Create HTML List from Array in PHP? Given an array containing some items, the task is to create an HTML list from an array in PHP. An HTML list is a collection of items enclosed within <ul> (unordered list) or <ol> (ordered list) tags. Each item in the list is enclosed within <li> (list item) tags. This article explo 3 min read How to display XML data in web page using PHP ? In this article, we are going to display data present in an XML file on a web page using PHP through the XAMPP server. PHP is a server-side scripting language that is mainly for processing web data. The XML stands for an extensible markup language. Requirements: XAMPP server Syntax: <root> 2 min read PHP code to store XML data into CSV In this article, we are going to store XML data into a CSV file using PHPH. XML stands for an extensible markup language. XML is similar to HTML but whereas in HTML, we can't define our own tags, but in XML we can define our own tags. Requirements: XAMPP Server. Structure of XML: <root> <ch 3 min read How to put string in array, split by new line in PHP ? Given a string concatenated with several new line character. The task is to split that string and store them into array such that strings are splitted by the newline.Example: Input : string is 'Ankit \n Ram \n Shyam' Output : Array ( [0] => Ankit [1] => Ram [2] => Shyam ) Using explode() Fu 2 min read Copy Elements of One Array into Another Array in PHP Copying elements from one array to another is a common operation in PHP. Copy elements operation is used to create a duplicate, merge arrays, or extract specific elements. In this article, we will explore various approaches to copying elements from one array into another in PHP.Table of ContentUsing 4 min read Convert multidimensional array to XML file in PHP Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file. Example: First, create a PHP Multidimensional 2 min read How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng 2 min read How to Convert XML to DataFrame in R? A Data Frame is a two-dimensional and tabular data structure in the R that is similar to a table in the database or an Excel spreadsheet. It is one of the most commonly used data structures for the data analysis in R with the columns representing the various and rows representing the observations.XM 4 min read Like