May 11, 2021 PHP
json_decode is a new PHP built-in function after php5.2.0 that encodes strings in JSON format. So how does this function work?
json_decode grammar rules:
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
json_decode accepts a string in JSON format and converts it to a PHP variable, when the parameter $assoc is TRUE, aray is returned, otherwise object is returned.
A string in JSON format
$json = '{"a":"php","b":"mysql","c":3}';
Where a is the key and php is the key value of a.
Instance:
<?php
$json = '{"a":"php","b":"mysql","c":3}';
$json_Class=json_decode($json);
$json_Array=json_decode($json, true);
print_r($json_Class);
print_r($json_Array);
?>
Program output:
stdClass Object (
[a] => php
[b] => mysql
[c] => 3 )
Array (
[a] => php
[b] => mysql
[c] => 3 )
Under the premise of the above code
Access the value of json_Class of the object type $
echo $json_Class->{'a'};
Program output: php
Access the value of the json_Array type $100."
echo $json_Array['a'];
Program output: php
Related tutorials:
PHP micro-class - theory of actual combat is not wrong