Tools, FAQ, Tutorials:
json_decode() - JSON to PHP Data Type Mapping
How data types are mapped from the JSON text string to the PHP variable when calling json_decode() function?
✍: FYIcenter.com
Data types are mapped from the JSON text string to the PHP variable
based on the following table, when calling json_decode() function:
JSON Data Type > PHP Data Type -------------- > ------------- null > null Boolean > Boolean Number > Int or Float String > String Array > Array Object > Object(stdClass) or Array
Here is a PHP example that shows you how json_decode() maps JSON data types to PHP data types:
<?php
# json_decode_datatype.php
# Copyright (c) FYIcenter.com
$json = 'null';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = 'true';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = '20.20';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = '2020';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = '"2020"';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = '["a","b","c"]';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
$json = '{"a":1,"b":2,"c":3}';
print("\nInput: ".$json."\n");
var_dump(json_decode($json));
?>
If you run the above PHP code through the PHP engine, you get the following output:
>\fyicenter\php\php.exe json_decode_datatype.php
Input: null
NULL
Input: true
bool(true)
Input: 20.20
float(20.2)
Input: 2020
int(2020)
Input: "2020"
string(4) "2020"
Input: ["a","b","c"]
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
Input: {"a":1,"b":2,"c":3}
object(stdClass)#1 (3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
⇒ json_decode() - JSON Array to PHP Array
2023-02-28, ∼2149🔥, 0💬
Popular Posts:
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
How to create a "Sign-up or Sign-in" user flow policy in my Azure AD B2C directory? If you want to b...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
How to add an API to an API product for internal testing on the Publisher Portal of an Azure API Man...
How To Move Uploaded Files To Permanent Directory in PHP? PHP stores uploaded files in a temporary d...