In PHP, converting JSON to an array is a very common operation. JSON is a lightweight data exchange format that represents data in the form of key-value pairs, and arrays are also a structure for storing data. Converting JSON to an array can help us process and manipulate data more conveniently.
Let’s introduce several methods to convert JSON to array in PHP.
Method 1: json_decode() function
PHP provides a built-in function json_decode() for converting JSON strings into PHP arrays. To use this function, you only need to pass a JSON string as a parameter. This function can also receive an optional parameter $assoc to control the returned array type.
The sample code is as follows:
$json = '{"name":"john", "age":30, "city":"New York"}'; $array = json_decode($json); print_r($array);
The output result is as follows:
Array ( [name] => john [age] => 30 [city] => New York )
In the above code, $json is a string containing JSON data, which is decoded through the json_decode() function. Convert it to PHP array. Since the $assoc parameter is not passed, an array of object type is returned by default. If you need to return an associative array, you can pass the second parameter as true.
$json = '{"name":"john", "age":30, "city":"New York"}'; $array = json_decode($json, true); print_r($array);
The output results are as follows:
Array ( [name] => john [age] => 30 [city] => New York )
Method 2: Use the json_decode() function and the file_get_contents() function
In addition to converting the JSON string into an array through the json_decode() function In addition, you can also obtain the JSON file contents through the file_get_contents() function and convert it into a PHP array using the json_decode() function.
The sample code is as follows:
$json = file_get_contents('data.json'); $array = json_decode($json); print_r($array);
In the above code, use the file_get_contents() function to obtain the contents of the JSON file named data.json, and then use the json_decode() function to convert it into a PHP array .
It should be noted that when using this method, BOM (Byte Order Mark) characters cannot exist in the JSON file, otherwise it will not be correctly parsed and converted into a PHP array.
Method 3: Use json_decode() function and curl library
If you need to obtain the JSON data returned by the remote server and convert it into a PHP array, you can use the curl library and json_decode() function .
The sample code is as follows:
$url = 'http://example.com/data.json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $json = curl_exec($ch); $array = json_decode($json); curl_close($ch); print_r($array);
In the above code, use the curl library to send a GET request to obtain the contents of the remote JSON file named data.json, and use the json_decode() function to Convert it to PHP array.
Summary
The above are several methods of converting JSON format data into PHP arrays. If you need to use JSON format data in a PHP project, you can use these methods to convert it into a PHP array for easier processing and manipulation.
The above is the detailed content of How to convert json into array in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
