An article explaining how to convert json array to php array

PHPz
Release: 2023-04-12 10:05:47
Original
744 people have browsed it

In the process of WEB development, we often use json format data exchange, and php is one of the most popular server-side languages. Therefore, it is necessary to convert the json array into a php array during development. This article will explain how to convert a json array into a php array.

1. Use the json_decode() function to convert a json array into a php array

The json_decode() function is PHP’s built-in function that converts a json string into a php variable. Using this function, you can simply convert a json array into a php array.

The specific implementation method is as follows:

$json = '[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"}]';
$array = json_decode($json, true);
print_r($array);
Copy after login

In the above code, we use a string to represent a json array and assign it to the variable $json. Then, we use the json_decode() function to convert $json into a php array, and then use the print_r() function to output the array.

The output result is as follows:

Array
(
    [0] => Array
        (
            [name] => apple
            [color] => red
        )

    [1] => Array
        (
            [name] => banana
            [color] => yellow
        )
)
Copy after login

As can be seen from the output result, we successfully converted the json array into a php array.

2. Handle json_decode() conversion failure

In the above code, we use the json_decode() function to convert the json array into a php array. However, if the json string is not properly formatted, or the string is empty, the json_decode() function will return null. At this time, we need to perform special processing to avoid causing program exceptions.

We can use the following code to handle the situation where json_decode() conversion fails:

$json = 'null';
$array = json_decode($json, true);
if (is_null($array)) {
    echo 'json格式错误或为空';
} else {
    print_r($array);
}
Copy after login

In the above code, we assign an empty json string to the variable $json. Then, we use the json_decode() function to convert $json into a php array. Next, we use the is_null() function to judge the conversion result. If the return result is null, it means that the json format is incorrect or empty. At this time, we use the echo statement to output the error message; otherwise, we use the print_r() function to output the array.

3. Use the foreach() function to traverse the php array

After converting the json array into a php array, we often need to process the php array. PHP arrays are often traversed using the foreach() function.

The following is a sample code that demonstrates how to use the foreach() function to traverse a php array:

$json = '[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"}]';
$array = json_decode($json, true);
foreach ($array as $value) {
    echo $value['name'].'的颜色是'.$value['color'].'<br>';
}
Copy after login

In the above code, we use the foreach() function to traverse the php array. We use each element of the array as the value of the $value variable, and then use the echo statement to output the "name" and "color" attribute values ​​of each element.

The output result is as follows:

apple的颜色是red
banana的颜色是yellow
Copy after login

4. Convert php array into json string

In addition to converting json array into php array, we often need to convert php array into Convert array to json string. Fortunately, PHP provides the json_encode() function, which can convert php arrays into json strings.

The following is a sample code that demonstrates how to convert a php array into a json string:

$array = array(
    array('name' => 'apple', 'color' => 'red'),
    array('name' => 'banana', 'color' => 'yellow')
);
$json = json_encode($array);
echo $json;
Copy after login

In the above code, we use a php array to represent a list of fruits. Then, we use the json_encode() function to convert the array into a json string and assign it to the variable $json. Finally, we use the echo statement to output $json.

The output result is as follows:

[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"}]
Copy after login

As can be seen from the output result, we successfully converted the php array into a json string.

Summary

This article introduces how to convert a json array into a php array, and handles the situation where json_decode() conversion fails; demonstrates how to use the foreach() function to traverse a php array; finally, We also covered how to convert a php array into a json string.

The above is the detailed content of An article explaining how to convert json array to php array. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!