![]() ![]() ![]() In this way, we can use the simplexml_load_file() function to load the XML file and convert it into an array. Finally, print the $arr variable using the print_r() function. In this method, the function jsondecode takes JSON encoded string and changes it into a PHP variable, whereas the jsonencode function returns a string which is encoded in a json format for a particular value. Assign the typecasted array to the $arr variable. Method 1 With the help of the jsondecode and jsonencode method. Next, create a $arr variable and typecast the $XML variable into an array. Assign the simplexml_load_file() to the variable with file.xml as the parameter. We can create a XML file and use its path in the simplexml_load_file() function.įor example, we can use the same XML file in the first method, save the XML file as file.xml, and create a variable $XML in PHP. The function is almost similar to the simplexml_load_string() function. The function takes the path of the XML file as the first parameter. Then, we can convert the object into an array by typecasting it. We can use the simplexml_load_file() function to interpret the XML into an object. Use the simplexml_load_file() Function and Typecast It Into an Array to Convert XML Into Array in PHP Output: Array ( => Array ( => Array ( => Jack => Hari ) ) => Array ( => Array ( => Sia => Paul ) ) ) $XML = simplexml_load_string($xmlstr, "SimpleXMLElement", LIBXML_NOCDATA) Thus, we can convert XML into a PHP array using these various PHP functions. Here, the LIBXML_NOCDATA constant merges the character data (CDATA) as text nodes. Do not forget to include TRUE as the second parameter of the json_decode() function. Then, convert the JSON to array with the json_decode() function. Next, convert $XML into JSON using the json_encode() function. In the function, set the $xmlstr as the first parameter and set SimpleXMLElement as the class and LIBXML_NOCDATA as the third parameter. Next, create another variable $XML and assign the simplexml_load_string() function to it. Ĭreate a variable $xmlstr and store all the XML as a string in the variable. For the final part, we can use the json_decode() function with the parameter TRUE to convert the JSON into an array.įor example, consider the following XML data. Next, we should convert the object into JSON using the json_encode() function. The third parameter of the function is the LIBXML constants. The function will return the object of the specified class. We can specify a class as the second option. The function takes the XML string as its first parameter. We can use the simplexml_load_string() function to interpret the XML string as an object. We can represent XML data as a PHP array with a series of conversions. Use the simplexml_load_string(), json_encode(), and json_decode() Functions to Convert XML Into an Array in PHP This tutorial will introduce how to convert XML into an array in PHP. Use the simplexml_load_file() Function and Typecast It Into an Array to Convert XML Into Array in PHP.Use the simplexml_load_string(), json_encode(), and json_decode() Functions to Convert XML Into an Array in PHP.Using this function in the below program. To make this data useful, we can use json_decode() to convert the JSON string to an array. The function file_get_contents() have only parsed the JSON data stored in a JSON file. The following program shows how to parse a JSON file. $length optional It is the maximum length of the file in bytes to be read. $startingPoint optional It specifies the starting point of the reading file. $customContext optional It is used to specify a custom context. Parameter Description $pathOfFile mandatory It specifies the path of the file $searchPath optional It specifies the path to search file. The detail of these parameters is as follows. file_get_contents($pathOfFile, $searchPath, $customContext, $startingPoint, $length) The correct syntax to use this function is as follows. By using this function, we can parse a JSON file to a string. The built-in function file_get_contents() is used to read a file and store it into a string. Use file_get_contents() Function to Parse a JSON File in PHP The contents of the JSON file used in the example codes are as follows. In this article, we will introduce the method to parse a JSON file in PHP.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |