Home  >  Article  >  Backend Development  >  How to convert array to json string in php

How to convert array to json string in php

PHPz
PHPzOriginal
2023-04-20 13:51:36870browse

PHP is a very convenient language for processing data, whether it is an array or JSON, it can be processed easily. In this article, we will explain how to convert an array in PHP into a JSON string.

1. Arrays in PHP

Array is a very common variable type in PHP. It is a collection of key-value pairs. In PHP, arrays can be defined using the following two syntaxes:

// 语法一
$array1 = array('value1', 'value2', 'value3');

// 语法二
$array2 = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];

As you can see, we can use the array() function or directly use square brackets [] to define an array.

2. JSON in PHP

JSON is a data exchange format that is based on JavaScript syntax, but can be used in many different programming languages. In PHP, we can use the json_encode() function to convert a PHP array into a JSON string, as shown below:

$array = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];

$jsonString = json_encode($array);
// 输出 {"key1":"value1","key2":"value2","key3":"value3"}

As you can see, we first define a PHP array, and then use the json_encode() function to convert Convert it to JSON string. Due to different structures, JSON-formatted strings cannot be processed directly by PHP.

3. Convert the array to a JSON string

When using the json_encode() function to convert a PHP array into a JSON string, you need to pay attention to the following points:

  1. The key in the array must be a string
  2. The value in the array can be any type of data, including arrays and objects
  3. The json_encode() function needs to use UTF-8 encoding, so Make sure to convert the string to UTF-8 encoding before processing

The following is a sample code to convert an associative array into a JSON string:

$person = [
  'name' => '张三',
  'age' => 25,
  'address' => [
    'province' => '广东省',
    'city' => '深圳市'
  ]
];

$jsonString = json_encode($person, JSON_UNESCAPED_UNICODE);
echo $jsonString;
// 输出 {"name":"张三","age":25,"address":{"province":"广东省","city":"深圳市"}}

As you can see, we first An associative array $person is defined, including a string value, an integer value and a nested array address. Then we use the json_encode() function to convert the $person array into a JSON-formatted string. Since we used the JSON_UNESCAPED_UNICODE option, there are no escaped Unicode characters in the output string.

4. Summary

In this article, we introduced how to convert an array in PHP into a JSON string, which can be easily achieved using the json_encode() function. When using the json_encode() function, you need to pay attention to the data type and encoding format in the array. I hope this article can be helpful to PHP developers.

The above is the detailed content of How to convert array to json string 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
Previous article:How to align php arraysNext article:How to align php arrays