Home > Backend Development > PHP Tutorial > Quickly decode specified JSON string via PHP

Quickly decode specified JSON string via PHP

藏色散人
Release: 2023-04-10 13:30:02
Original
2967 people have browsed it

In the previous article, I introduced to you how to use an unordered list to display the values ​​​​in a PHP array. Friends who are interested can click ->《Teach you to use an unordered list to display the values ​​​​Values ​​in PHP array》.

This article introduces you to a new little knowledge, which is to decode the specified JSON string through PHP code.

First look at the JSON sample code, as follows:

{"Title": "The Cuckoos Calling",
"Author": "Robert Galbraith",
"Detail": {
"Publisher": "Little Brown"
}}
Copy after login

The question is how to decode this JSON code?

Very simple.

The PHP code is as follows:

<?php
function w3rfunction($value,$key)
{
    echo "$key : $value"."<br>";
}
$a = &#39;{"Title": "The Cuckoos Calling",
"Author": "Robert Galbraith",
"Detail":
{ 
"Publisher": "Little Brown"
 }
  }&#39;;
$j1 = json_decode($a,true);
array_walk_recursive($j1,"w3rfunction");
Copy after login

The running results are as follows:

Quickly decode specified JSON string via PHP

Here we must introduce to you a key function json_decode();

json_decodeThe function is to decode the string in JSON format.

The syntax is:

json_decode(
    string $json,
    bool $assoc = false,
    int $depth = 512,
    int $options = 0
): mixed
Copy after login

Can accept a JSON encoded string and convert it into a PHP variable. The return value is returned in json through the appropriate PHP type. data. The values ​​true, false and null will return true, false and null accordingly. If json cannot be decoded, or the depth of the encoded data exceeds the recursion limit, null will be returned.

The parameters respectively represent:

json: a string in json string format to be decoded. This function can only process UTF-8 encoded data.

assoc: When this parameter is true, array will be returned instead of object.

depth: Specify the recursion depth.

options: Mask consisting of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.

Now that the introduction is here, let me tell you about the json_encode function. This function is used to JSON encode variables, which is exactly the opposite of json_decode. For those who are interested You can read the article "Detailed explanation of json_encode() and json_decode() functions in php".

Finally, I would like to recommend to you the latest free course on our platform "Entering the World of PHP from 0"~ Come and learn!

The above is the detailed content of Quickly decode specified JSON string via PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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