In PHP, array is one of the common data structures. Now, due to the increasing popularity of data exchange and front-end separation, JSON data format is widely used. So how do you convert an array in PHP into a JSON object? This article will give the answer.
1. Use the PHP built-in function json_encode ().
PHP provides a built-in function json_encode () to convert PHP arrays into JSON objects. The json_encode () function takes a PHP variable as a parameter and encodes the variable into a JSON format string. The following is an example of converting a PHP array into a JSON object using the json_encode () function.
$my_array = [1, 2, 'hello', 'world']; $json_string = json_encode($my_array); echo $json_string;
The output of the above code is as follows:
[1,2,"hello","world"]
It should be noted that the second optional parameter $option of the json_encode () function is 0 by default, indicating that there is no indentation in the output result. You can make the result more readable by setting this parameter to JSON_PRETTY_PRINT.
2. Use the PHP built-in function json_decode ().
Corresponding to json_encode () function, PHP also provides json_decode () function, which is used to convert JSON format strings into PHP arrays. The following is an example of using json_decode () function to convert JSON objects into PHP arrays.
$json_string = '[1,2,"hello","world"]'; $my_array = json_decode($json_string); var_dump($my_array);
The output result is:
array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(5) "hello" [3]=> string(5) "world" }
It should be noted that the json_decode () function converts JSON strings into stdClass objects by default. If you want to convert it into a PHP array, you can set the second parameter of the json_decode () function to true, as shown below:
$json_string = '{"name": "Tom", "age": 18}'; $my_array = json_decode($json_string, true); var_dump($my_array);
The output result is:
array(2) { ["name"]=> string(3) "Tom" ["age"]=> int(18) }
3. Use PHP class library
If you want to edit JSON data more advanced, you can use the JSON class library in PHP, such as pecl-json or jsonlint. These class libraries provide more options and functions than json_encode () and json_decode () functions.
For example, using the pecl-json class library, you can easily convert PHP arrays into JSON objects:
use JsonSerializable; class MyArray implements JsonSerializable{ private $arr; public function __construct($arr = []) { $this->arr = $arr; } public function jsonSerialize() { return $this->arr; }} $my_array = new MyArray([1, 2, 'hello', 'world']); $json_string = json_encode($my_array); echo $json_string;
The output result is:
[1,2,"hello","world"]
It should be noted that when converting a PHP object into a JSON object, this PHP object must implement the JsonSerializable interface. After the JsonSerializable interface is implemented, the json_encode () function will call the interface method jsonSerialize () to convert PHP objects into JSON objects.
Conclusion
This article introduces how to convert an array into a JSON object in PHP. By using PHP built-in functions json_encode () and json_decode (), we can easily convert the basic data format. If you need more advanced JSON data editing operations, you can use the JSON class library in PHP. Take a look at our example again. Without using other class libraries, we can convert arrays and json to each other like this:
$my_array = [1, 2, 'hello', 'world']; $json_string = json_encode($my_array); $result_array = json_decode($json_string, true);
So simple, so convenient!
Copyright Description:No reproduction without permission。
Knowledge sharing community for developers。
Let more developers benefit from it。
Help developers share knowledge through the Internet。
Follow us