Home > Backend Development > PHP Problem > How to convert json data to array in php

How to convert json data to array in php

PHPz
Release: 2023-04-20 11:03:27
Original
2777 people have browsed it

In Web development, JSON (JavaScript Object Notation), as a lightweight data exchange format, has been widely used due to its simple and easy-to-use characteristics. When we receive data in JSON format from the backend, sometimes we need to convert it into an array to facilitate our data processing. In PHP, there are many ways to convert JSON data into arrays.

Method 1: Use the json_decode function

The json_decode function is a built-in function of the JSON decoder in PHP, which can convert a JSON format string into a PHP array. The following is a sample code:

$json_str = '{"name":"Tom","age":20}';
$array = json_decode($json_str, true);
var_dump($array);
Copy after login

This code converts the JSON string {"name":"Tom","age":20} into a PHP array and outputs it. Among them, if the second parameter is true, it means that an associative array is returned, otherwise an object is returned.

Method 2: By reading JSON data from a file

If we have a file that saves JSON data, we can use the file_get_contents function to read the file content and use the json_decode function to parse the string:

$json_str = file_get_contents('data.json');
$array = json_decode($json_str, true);
var_dump($array);
Copy after login

Among them, data.json file is the name of the file that saves JSON data. This method also supports the second parameter being true, which means converting the JSON object into a PHP associative array.

Method 3: Use cURL to obtain JSON data from the API interface

Sometimes you need to call JSON data through the API interface. You can use cURL to initiate an HTTP request and obtain the returned JSON data, and then use the json_decode function Convert it to an array.

// 设置cURL选项
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://some-api.com/data");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 执行请求并获取结果
$result = curl_exec($curl);
// 关闭cURL
curl_close($curl);
// 解析JSON数据
$array = json_decode($result, true);
var_dump($array);
Copy after login

One thing to note about this method is that the cURL plug-in needs to be installed and turned on in advance.

Summary

Converting JSON data into an array is a common operation in web development. PHP provides a variety of methods to complete this task, including using the json_decode function to parse JSON strings and reading JSON files. Get JSON data and get JSON data from API interface through cURL, etc. You can choose different methods according to specific scenarios to quickly complete the processing of JSON data.

The above is the detailed content of How to convert json data to array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template