Home  >  Article  >  Backend Development  >  How to convert php array into json object

How to convert php array into json object

PHPz
PHPzOriginal
2023-04-17 16:36:45979browse

In PHP, array is one of the common data structures. Now, due to the increasing popularity of data exchange and front-end and back-end separation, it can be said that the JSON data format is widely used. So how to convert an array in PHP into a JSON object? This article will give the answer.

1. Use 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 accepts a PHP variable as a parameter and encodes the variable into a JSON-formatted string. Below is an example of using the json_encode() function to convert a PHP array into a JSON object.

$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 defaults to 0, which means there is no indentation in the output result. . You can make the results more readable by setting this parameter to JSON_PRETTY_PRINT.

2. Use the PHP built-in function json_decode()

Corresponding to the json_encode() function, PHP also provides the json_decode() function for converting JSON format strings into PHP arrays. Below is an example of using the json_decode() function to convert a JSON object into a PHP array.

$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 to a PHP array, you can set the second parameter of the json_decode() function to true, as follows:

$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 perform more advanced editing operations on JSON data, you can use the JSON class library in PHP, such as pecl-json or jsonlint. These libraries provide more options and functionality than the json_encode() and json_decode() functions.

For example, using the pecl-json class library, you can easily convert a PHP array into a JSON object:

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 to a JSON object, the PHP object must implement the JsonSerializable interface. After implementing the JsonSerializable interface, the json_encode() function will call the interface method jsonSerialize() to convert the PHP object into a JSON object.

Conclusion

This article explains how to convert an array to a JSON object in PHP. By using PHP's built-in functions json_encode() and json_decode(), we can easily convert basic data formats. If you need to perform more advanced JSON data editing operations, you can use the JSON class library in PHP. Let’s look at our example again. Without using other class libraries, you can convert arrays to json 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!

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