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

How to convert json into array in php

PHPz
PHPzOriginal
2023-04-24 14:51:125715browse

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!

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