Home > Article > Backend Development > How to convert php+xml and array to each other
In Web development, XML and array formats are often used to store and transmit data. XML is a markup language that is often used for data description and interactive transmission, while arrays are a data structure that are often used for data processing and operations in programs. In the PHP language, we can convert PHP to XML and arrays, and this conversion is very convenient and fast.
This article mainly introduces the mutual conversion methods between PHP, XML and arrays in PHP, including the conversion methods from XML to arrays and from arrays to XML.
1. Mutual conversion between PHP and XML
In PHP, we can use the SimpleXML extension to realize mutual conversion between PHP and XML. SimpleXML is a PHP built-in extension for processing data in XML files. It can convert XML files into PHP objects or arrays, and also convert PHP objects or arrays into XML files.
To convert XML file to PHP array, you need to use the simplexml_load_file function in the SimpleXML extension. The simplexml_load_file function can read the XML file at the specified path and return a SimpleXMLElement object, and then convert the information in the XML file into a PHP array by traversing the SimpleXMLElement object.
The following is a code example to convert an XML file into a PHP array:
$xmlStr = <<<XML <?xml version='1.0'?> <menu> <item> <name>Chicken Curry</name> <price>8.95</price> <description>A spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>Beef Stir Fry</name> <price>10.95</price> <description>A savory dish with beef, mushrooms, and peppers</description> </item> </menu> XML; $xml = simplexml_load_string($xmlStr); $menu = []; foreach ($xml->item as $item) { $menu[] = [ 'name' => (string) $item->name, 'price' => (float) $item->price, 'description' => (string) $item->description, ]; } print_r($menu);
Run the above code, the output result is:
Array ( [0] => Array ( [name] => Chicken Curry [price] => 8.95 [description] => A spicy dish with chicken, carrots, and potatoes ) [1] => Array ( [name] => Beef Stir Fry [price] => 10.95 [description] => A savory dish with beef, mushrooms, and peppers ) )
, the data in the array needs to be organized according to the syntax rules of XML. In PHP, we can use the SimpleXMLElement class to create XML elements, and use a foreach loop to iterate through the data in the array, add the data to the XML elements one by one, and finally output or save the XML file to a specified location through the asXML method of the SimpleXMLElement object. .
The following is a code example to convert a PHP array into an XML file:
$menu = [ [ 'name' => 'Chicken Curry', 'price' => 8.95, 'description' => 'A spicy dish with chicken, carrots, and potatoes' ], [ 'name' => 'Beef Stir Fry', 'price' => 10.95, 'description' => 'A savory dish with beef, mushrooms, and peppers' ] ]; $xml = new SimpleXMLElement('<menu></menu>'); foreach ($menu as $item) { $menuItem = $xml->addChild('item'); $menuItem->addChild('name', $item['name']); $menuItem->addChild('price', $item['price']); $menuItem->addChild('description', $item['description']); } $xmlStr = $xml->asXML(); echo $xmlStr;
Run the above code, the output result is:
<?xml version="1.0"?> <menu> <item> <name>Chicken Curry</name> <price>8.95</price> <description>A spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>Beef Stir Fry</name> <price>10.95</price> <description>A savory dish with beef, mushrooms, and peppers</description> </item> </menu>
2. Interaction between PHP and arrays Conversion
In PHP, we can use the json_encode and json_decode functions to convert PHP to arrays. The json_encode function converts an array in PHP to a JSON-formatted string and returns the converted string; the json_decode function converts a JSON-formatted string into an array in PHP.
To convert a PHP array into a string in JSON format, you need to use the json_encode function. The json_encode function can convert an array in PHP into a JSON-formatted string and return a value representing the JSON string.
The following is a code example to convert a PHP array into a string in JSON format:
$menu = [ [ 'name' => 'Chicken Curry', 'price' => 8.95, 'description' => 'A spicy dish with chicken, carrots, and potatoes' ], [ 'name' => 'Beef Stir Fry', 'price' => 10.95, 'description' => 'A savory dish with beef, mushrooms, and peppers' ] ]; $json = json_encode($menu); echo $json;
Run the above code, the output result is:
[{"name":"Chicken Curry","price":8.95,"description":"A spicy dish with chicken, carrots, and potatoes"},{"name":"Beef Stir Fry","price":10.95,"description":"A savory dish with beef, mushrooms, and peppers"}]
To convert a string in JSON format into a PHP array, you need to use the json_decode function. The json_decode function can convert a JSON-formatted string into an array in PHP and return a value representing the PHP array.
The following is a code example to convert a JSON formatted string into a PHP array:
$json = '[{"name":"Chicken Curry","price":8.95,"description":"A spicy dish with chicken, carrots, and potatoes"},{"name":"Beef Stir Fry","price":10.95,"description":"A savory dish with beef, mushrooms, and peppers"}]'; $menu = json_decode($json, true); print_r($menu);
Run the above code, the output result is:
Array ( [0] => Array ( [name] => Chicken Curry [price] => 8.95 [description] => A spicy dish with chicken, carrots, and potatoes ) [1] => Array ( [name] => Beef Stir Fry [price] => 10.95 [description] => A savory dish with beef, mushrooms, and peppers ) )
3. PHP and Mutual conversion of XML arrays
In PHP, we can use the SimpleXML extension and the json_encode/json_decode function to achieve mutual conversion between PHP, XML and arrays. Specifically, we can first convert the XML file into a PHP array, and then convert the PHP array into a JSON-formatted string; similarly, we can also convert a JSON-formatted string into a PHP array, and then convert the PHP array into a SimpleXMLElement Object, and finally get an XML file.
The following is a complete code example for conversion between PHP and XML arrays:
$xmlStr = <<<XML <?xml version='1.0'?> <menu> <item> <name>Chicken Curry</name> <price>8.95</price> <description>A spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>Beef Stir Fry</name> <price>10.95</price> <description>A savory dish with beef, mushrooms, and peppers</description> </item> </menu> XML; // 将XML文件转换为PHP数组 $xml = simplexml_load_string($xmlStr); $menu = []; foreach ($xml->item as $item) { $menu[] = [ 'name' => (string) $item->name, 'price' => (float) $item->price, 'description' => (string) $item->description, ]; } // 将PHP数组转换为JSON格式的字符串 $json = json_encode($menu); // 将JSON格式的字符串转换为PHP数组 $menu = json_decode($json, true); // 将PHP数组转换为SimpleXMLElement对象 $xml = new SimpleXMLElement('<menu></menu>'); foreach ($menu as $item) { $menuItem = $xml->addChild('item'); $menuItem->addChild('name', $item['name']); $menuItem->addChild('price', $item['price']); $menuItem->addChild('description', $item['description']); } // 输出XML文件 $xmlStr = $xml->asXML(); echo $xmlStr;
Run the above code, the output result is:
<?xml version="1.0"?> <menu> <item> <name>Chicken Curry</name> <price>8.95</price> <description>A spicy dish with chicken, carrots, and potatoes</description> </item> <item> <name>Beef Stir Fry</name> <price>10.95</price> <description>A savory dish with beef, mushrooms, and peppers</description> </item> </menu>
Summary
The above is the method of converting PHP to XML and arrays, which is very simple and practical. Whether in front-end or back-end data transmission or data processing, we can perform corresponding conversions according to actual needs. It should be noted that when converting between XML and arrays, pay attention to the structure and format of the data to avoid unnecessary errors.
The above is the detailed content of How to convert php+xml and array to each other. For more information, please follow other related articles on the PHP Chinese website!