Home  >  Article  >  Backend Development  >  How to convert array to encoding in php

How to convert array to encoding in php

PHPz
PHPzOriginal
2023-04-25 09:06:19384browse

PHP is a very popular programming language when it comes to developing web applications. PHP's arrays are an indispensable tool when working with data. However, when we need to pass PHP arrays to other languages, or pass them over the network, we need to convert the arrays into an encoding format. This article explains how PHP converts arrays to JSON, XML, and serialization formats.

  1. Convert array to JSON format

JSON (JavaScript Object Notation) is a lightweight data exchange format for data exchange. It is easy to read and write and is often used to transfer data between web applications. PHP provides json_encode() and json_decode() functions, which can easily convert PHP arrays into JSON format, and can also convert JSON format data into PHP arrays.

The following is a sample code to convert a PHP array to JSON format:

$data = array(
    'name' => 'John',
    'age' => 30,
    'city' => 'New York'
);
$json = json_encode($data);
echo $json;

The output result is as follows:

{"name":"John","age":30,"city":"New York"}

In this example, we first define an array containing name PHP array $data with three elements: , age and city. Then use the json_encode() function to convert it to JSON format. The output result is a JSON string containing the array elements.

In order to convert JSON formatted data back to a PHP array, we can use the json_decode() function. The following is a sample code to convert JSON format data into a PHP array:

$json = '{"name":"John","age":30,"city":"New York"}';
$data = json_decode($json, true);
print_r($data);

The output result is:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

In this example, we first define a JSON string. Then use the json_decode() function to convert it to a PHP array, and set the second parameter to true to convert the JSON format to an associative array. Finally, use the print_r() function to output the contents of the PHP array.

  1. Convert array to XML format

XML (Extensible Markup Language) is a standard for defining your own markup language. It is very similar to the HTML markup language, but is primarily used for data exchange, data storage, and web services. SimpleXML extension is provided in PHP, which can be used to convert PHP arrays to XML format, and can also convert XML format data to PHP arrays.

The following is a sample code to convert a PHP array to XML format:

$data = array(
    'name' => 'John',
    'age' => 30,
    'city' => 'New York'
);
$xml = new SimpleXMLElement('');
array_walk_recursive($data, array($xml, 'addChild'));
echo $xml->asXML();

The output result is as follows:



  John
  30
  New York

In this example, we first define an array containing name PHP array $data with three elements: , age and city. Then use the SimpleXMLElement class to create an XML element named root. Each element in the $data array is then added to the XML tree using the array_walk_recursive() function. Finally, use the asXML() function to output the XML tree into a string.

In order to convert XML formatted data into a PHP array, we can use the simplexml_load_string() or simplexml_load_file() function provided by the SimpleXML extension. The following is a sample code that uses the simplexml_load_string() function to convert an XML string to a PHP array:

$xml = '

  John
  30
  New York
';

$data = simplexml_load_string($xml);
echo '
';
print_r($data);
echo '
';

The output result is:

SimpleXMLElement Object
(
    [name] => John
    [age] => 30
    [city] => New York
)

In this example, we first define an XML character string. Then use the simplexml_load_string() function to convert it to PHP's SimpleXMLElement object, and finally use the print_r() function to output the contents of the PHP array.

  1. Convert array to serialized format

In addition to JSON and XML formats, PHP also provides a way to convert PHP arrays to strings, that is, array sequences change. Serialization is the process of converting PHP variables (primitive data types, arrays, and objects) into strings in order to pass data between programs. PHP provides serialize() and unserialize() functions to implement serialization and deserialization of PHP variables.

The following is a sample code to serialize a PHP array into a string:

$data = array(
    'name' => 'John',
    'age' => 30,
    'city' => 'New York'
);
$serialized = serialize($data);
echo $serialized;

The output is as follows:

a:3:{s:4:"name";s:4:"John";s:3:"age";i:30;s:4:"city";s:8:"New York";}

In this example, we first define a PHP array $data containing three elements: name, age and city. Then serialize it to a string using the serialize() function. The output result is a string containing the serialized array elements.

In order to deserialize the serialized string into a PHP array, we can use the unserialize() function. The following is an example code for deserializing a serialized string into a PHP array:

$serialized = 'a:3:{s:4:"name";s:4:"John";s:3:"age";i:30;s:4:"city";s:8:"New York";}';
$data = unserialize($serialized);
print_r($data);

The output result is:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

In this example, we first define a serialization the string after. Then use the unserialize() function to deserialize it into a PHP array and use the print_r() function to output the contents of the PHP array.

Summary

In this article, we covered how PHP converts arrays to JSON, XML, and serialization formats. These formats are among the common formats used for data exchange, making it easy to transfer data between different platforms and languages. It is necessary to select a suitable format for data processing according to actual needs.

The above is the detailed content of How to convert array to encoding in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn