Home Backend Development PHP Problem Convert php array to json array

Convert php array to json array

May 05, 2023 pm 08:22 PM

For developers, PHP is a very convenient programming language. In the development of web applications, PHP arrays and JSON arrays are very commonly used data structures. Here, we will delve into how to convert an array into a JSON formatted array in PHP.

PHP 5.2.0 and above supports using the json_encode() function to convert PHP arrays into JSON format, and also provides some optional parameters to adjust the behavior of JSON arrays.

The following is a simple PHP array:

$php_array = array(
    'fruit' => 'apple',
    'number' => 10,
    'price' => 2.5
);
Copy after login

This PHP array can be converted to JSON format using the json_encode() function:

$json_array = json_encode($php_array);
Copy after login

The above code Convert $php_array to JSON format and assign it to the $json_array variable. Now, $json_array saves the following JSON string:

{"fruit":"apple","number":10,"price":2.5}
Copy after login

We can use the json_decode() function to convert it back:

$decoded = json_decode($json_array);
Copy after login

Now , we can access the elements of the original PHP array using $decoded variables:

echo $decoded->fruit; // 输出 "apple"
echo $decoded->number; // 输出 10
echo $decoded->price; // 输出 2.5
Copy after login

Use optional parameters to change the behavior of the JSON array:

After using json_encode( ) function, we can also pass some optional parameters to change the behavior of the JSON array.

  1. JSON_PRETTY_PRINT: Used to format a JSON array to make it easier to read. Here is the JSON string generated after applying this option:
{
    "fruit": "apple",
    "number": 10,
    "price": 2.5
}
Copy after login
  1. JSON_FORCE_OBJECT: Cast a PHP array to an object.
$php_array = array('apple', 'banana', 'orange');
$json_array = json_encode($php_array, JSON_FORCE_OBJECT);

//生成json数组,
//{
//    "0": "apple",
//    "1": "banana",
//    "2": "orange"
//}
Copy after login
  1. JSON_UNESCAPED_UNICODE: Causes the generated JSON array to not be UTF-8 encoded.
$php_array = array('西瓜', '西红柿', '黄瓜');
$json_array = json_encode($php_array, JSON_UNESCAPED_UNICODE);

// 生成的 JSON 字符串:["西瓜","西红柿","黄瓜"]
Copy after login

Summary:

It is very simple to use the json_encode() function to convert a PHP array into a JSON format array. When retrieving a PHP array from a JSON formatted array, we can use the json_decode() function. You can also use options to change the behavior of JSON arrays to meet specific needs.

The above is the detailed content of Convert php array to json array. 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 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
1663
14
PHP Tutorial
1263
29
C# Tutorial
1236
24