Home > Article > Backend Development > How to convert php arrays to each other
The use of arrays in PHP is very extensive, but in different situations, the format of arrays may be different, so we need to convert arrays to and from each other to use them better. In this article, we will introduce different ways to convert arrays to and from PHP arrays.
1. Overview of PHP array conversions
In PHP, there are many types of arrays, including associative arrays, index arrays, etc. Different types of arrays have different operations and applications, so in different situations, we need to convert arrays to and from each other to use them better. In the following sections, we will discuss different types of PHP array conversions.
2. Convert PHP associative array to JSON format
JSON format (JavaScript Object Notation) is a lightweight data format that can easily transfer data between different computer systems . You can convert a PHP array to JSON format as follows:
$a = array(
"name" => "John", "age" => 30, "city" => "New York"
);
$json = json_encode($a);
echo $json;
In the above code, we first define a PHP associative array $a, and then use the json_encode() function to convert the array into JSON format. Finally, we use the echo command to print out the JSON data.
3. Convert PHP index array to JSON format
In addition to associative arrays, there is also an array of this type in PHP. You can use the above method to convert it to JSON format. Method As follows:
$a = array("red", "green", "blue");
$json = json_encode($a);
echo $json;
In the above code, we first define an index array $a, and then use the json_encode() function to convert the array into JSON format. Finally, we use the echo command to print out the JSON data.
4. Convert PHP associative array to XML format
Similar to JSON format, XML format is also a format that can easily transmit data between different computer systems. We can convert a PHP array to XML format as follows:
$a = array(
"name" => "John", "age" => 30, "city" => "New York"
);
$xml = new SimpleXMLElement('
array_walk_recursive($a, array($xml, 'addChild'));
echo $xml->asXML();
In the above code, we use The SimpleXMLElement class converts the associative array $a to XML format. Use the array_walk_recursive function to add the keys and values of the associative array as XML elements, and finally use the echo command to print out the XML data.
5. Convert PHP index array to XML format
Similarly, we can also convert PHP index array to XML format as follows:
$a = array( "red", "green", "blue");
$xml = new SimpleXMLElement('
array_walk_recursive($a, array($xml, 'addChild '));
echo $xml->asXML();
In the above code, we use the SimpleXMLElement class to convert the index array $a into XML format. Use the array_walk_recursive function to add array elements as XML elements, and finally use the echo command to print out the XML data.
6. Convert PHP JSON format to associative array
Sometimes, we need to convert JSON format data into PHP associative array. This can be achieved through the json_decode() function. The method is as follows :
$json = '{"name":"John", "age":30, "city":"New York"}';
$array = json_decode($json, true) ;
print_r($array);
In the above code, we first define a JSON data, and then use the json_decode() function to convert it into a PHP array. Use the print_r() function to print out the elements of an array in a human-readable way.
7. Convert PHP JSON format to index array
Similarly, we can also convert JSON format data into PHP index array as follows:
$json = '["red", "green", "blue"]';
$array = json_decode($json, true);
print_r($array);
In the above code , we first define a JSON data, and then use the json_decode() function to convert it into a PHP array. Use the print_r() function to print out the elements of an array in a human-readable way.
8. Convert PHP XML format to associative array
In some cases, we need to convert XML format data into PHP associative array, which can be achieved by using the SimpleXML extension. The method is as follows:
$xml = '
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$array = json_decode($json, true);
print_r($array);
In the above code, we first define an XML data, and then use the simplexml_load_string() function to convert it into a SimpleXMLElement object. Next, we use the json_encode() function to convert the SimpleXMLElement object into JSON format, and finally use the json_decode() function to convert the JSON format into a PHP array. Use the print_r() function to print out the elements of an array in a human-readable way.
9. Convert PHP XML format to index array
Similarly, we can also convert XML format data into PHP index array as follows:
$xml = '
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$array = json_decode($json, true);
print_r($array);
In the above In the code, we first define an XML data, and then use the simplexml_load_string() function to convert it into a SimpleXMLElement object. Next, we use the json_encode() function to convert the SimpleXMLElement object into JSON format, and finally use the json_decode() function to convert the JSON format into a PHP array. Use the print_r() function to print out the elements of an array in a human-readable way.
Conclusion
In PHP, arrays are widely used, but on different occasions, arrays may need to be converted to different formats. Through the methods provided in this article, we can convert different types of arrays to each other, including associative arrays, index arrays, JSON format and XML format. The conversion of various arrays can better meet our needs for data and improve the efficiency of PHP programming.
The above is the detailed content of How to convert php arrays to each other. For more information, please follow other related articles on the PHP Chinese website!