Home  >  Article  >  Backend Development  >  Convert php to json array

Convert php to json array

PHPz
PHPzOriginal
2023-05-24 18:24:09480browse

PHP is a very popular server-side programming language that is widely used in website development. In the actual development process, we usually need to convert PHP arrays into JSON arrays for processing and display on the front end. The following will introduce in detail how to convert a PHP array into a JSON array.

1. What is JSON?

JSON is the abbreviation of JavaScript Object Notation. It is a lightweight data exchange format. Because of its simplicity, clarity, ease of use and readability, It is maintainable and streamable, so it is widely used in front-end data interaction and interface transmission.

The syntax rules of JSON are very simple. It consists of key-value pairs: the key name must be a string, and the value can be a string, number, true, false, null, array, or object. Generally expressed as {key1:value1, key2:value2, ...}, where different key-value pairs are separated by commas, with the key name on the left and the value on the right.

Example:

{
"name": "Tom",
"age": 18,
"hobby": ["reading", "music" ],
"info": {

"address": "Beijing",
"telephone": "1234567890"

}
}

2. How to convert PHP array to JSON array

Convert PHP array to JSON There are two methods for arrays: json_encode() and json_decode().

json_encode() method: Convert PHP array to JSON array

json_encode() method can convert PHP array to JSON array. The syntax of this method is very simple:

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Parameter description:

$value: required, PHP array that needs to be converted into JSON.

$options: Optional, as the second parameter of json_encode(), used to control some options of the conversion process.

$depth: Optional, as the third parameter of json_encode(), set the recursion depth.

Let’s look at an example:

$arr ​​= array(

"name" => "Tom",
"age" => 18,
"hobby" => array("reading", "music"),
"info" => array(
    "address" => "Beijing",
    "telephone" => "1234567890"
)

);

echo json_encode($arr);

The output result is:

{"name":"Tom","age":18,"hobby":["reading","music"],"info":{"address": "Beijing","telephone":"1234567890"}}

You can see that the output result has converted the PHP array into a string in JSON format.

json_decode() method: Convert JSON array into PHP array

The json_decode() method can convert JSON array into PHP array, which facilitates our subsequent operations. The syntax of this method is also very simple:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

Parameter description:

$json: required, the JSON string that needs to be converted.

$assoc: Optional, as the second parameter of json_decode(), if true, a PHP object is returned, otherwise a PHP associative array is returned.

$depth: Optional, as the third parameter of json_decode(), set the recursion depth.

$options: Optional, as the fourth parameter of json_decode(), used to control some options of the conversion process.

Let’s look at an example:

$str = '{"name":"Tom","age":18,"hobby":["reading","music" ],"info":{"address":"Beijing","telephone":"1234567890"}}';

$arr ​​= json_decode($str, true);

print_r ($arr);

The output result is:

Array
(

[name] => Tom
[age] => 18
[hobby] => Array
    (
        [0] => reading
        [1] => music
    )

[info] => Array
    (
        [address] => Beijing
        [telephone] => 1234567890
    )

)

You can see that the output result has been in JSON format The string is converted into a PHP array.

Summary:

The above is the method to convert a PHP array into a JSON array. You can also use the matching json_decode() method to convert a JSON array into a PHP array. In actual development, we need to choose the appropriate conversion method according to specific needs in order to operate data more flexibly.

The above is the detailed content of Convert php to json array. 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