Home  >  Article  >  Backend Development  >  What is the function to convert php array to josn

What is the function to convert php array to josn

PHPz
PHPzOriginal
2023-04-26 10:29:32534browse

In PHP, we often need to convert arrays into JSON format strings to facilitate data transfer between different applications. PHP provides a built-in function json_encode() to implement this function. Let's take a look at the usage of this function.

json_encode()The function can convert a PHP array into a JSON string. The syntax is as follows:

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

Parameter description:

  • $value: The value that needs to be encoded can be a value, array or object.
  • $options: Optional parameters, used to set encoding options, affecting the encoding processing method. The default is 0, which means no special options are used.
  • $depth: Optional parameter used to set the maximum recursion depth. The default is 512, which means the depth does not exceed 512.

Sample code:

<?php
// 定义一个PHP关联数组
$fruits = array(
    &#39;apple&#39; => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

// 将PHP数组转换为JSON格式字符串
$json = json_encode($fruits);

// 输出JSON字符串
echo $json;
?>

Output result:

{"apple":"red","banana":"yellow","orange":"orange"}

As shown in the above code, we first define a PHP associative array$fruits, which contains several fruits and their colors. Then use the json_encode() function to convert the array into a JSON string and output the result.

As can be seen from the output, the converted JSON string has the same structure as the original PHP array, except that each key-value pair is enclosed in double quotes and separated by commas.

In actual applications, we may need to further process the JSON string, such as parsing it to obtain the data. PHP provides a built-in function json_decode() to parse JSON strings.

Sample code:

Output result:

array(3) {
  ["apple"]=>
  string(3) "red"
  ["banana"]=>
  string(6) "yellow"
  ["orange"]=>
  string(6) "orange"
}

As shown in the above code, we first define a JSON string $json_str, Several fruits and their colors are included. Then use the json_decode() function to parse the JSON string into a PHP associative array and output the array.

As can be seen from the output results, the parsed PHP array has the same structure as the original JSON string, except that the key-value pairs are connected using the => symbol. It should be noted that the second parameter of the json_decode() function can be used to control whether the decoding result should be converted into an associative array. If true is passed in, the decoding result will be converted into an associative array. It is an associative array, otherwise it will be converted into a PHP object.

The above is the detailed content of What is the function to convert php array to josn. 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