Home Backend Development PHP Problem How to convert php array into json object

How to convert php array into json object

Apr 17, 2023 pm 04:36 PM

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;
Copy after login

The output of the above code is as follows:

[1,2,"hello","world"]
Copy after login
Copy after login

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);
Copy after login

The output result is:

array(4) { [0]=> int(1) [1]=> int(2) [2]=> string(5) "hello" [3]=> string(5) "world" }
Copy after login

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);
Copy after login

The output result is:

array(2) { ["name"]=> string(3) "Tom" ["age"]=> int(18) }
Copy after login

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;
Copy after login

The output result is:

[1,2,"hello","world"]
Copy after login
Copy after login

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);
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24