Tools, FAQ, Tutorials:
json_encode() - PHP to JSON Data Type Mapping
How data types are mapped from the PHP variable to the JSON text string when calling the json_encode() function?
✍: FYIcenter.com
Data types are mapped from the PHP variable to the JSON text string
based on the following table, when calling the json_encode() function:
PHP Data Type > JSON Data Type ------------- > ------------- null > null Boolean > Boolean Int or Float > Number String > String Array > Array Object > Object Associative Array > Object
Here is a PHP example that shows you how json_encode() maps PHP data types to JSON data types:
<?php
# json_encode_datatype.php
# Copyright (c) FYIcenter.com
$var = null;
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = true;
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = 20.20;
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = 2020;
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = "2020";
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = array("a","b","c");
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
$var = new stdClass();
$var->a = "1";
$var->b = "2";
$var->c = "3";
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var)."\n");
?>
If you run the above PHP code through the PHP engine, you get the following output:
>\fyicenter\php\php.exe json_encode_datatype.php
Input: NULL
Output: null
Input: bool(true)
Output: true
Input: float(20.2)
Output: 20.2
Input: int(2020)
Output: 2020
Input: string(4) "2020"
Output: "2020"
Input: array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
Output: ["a","b","c"]
Input: object(stdClass)#1 (3) {
["a"]=>
string(1) "1"
["b"]=>
string(1) "2"
["c"]=>
string(1) "3"
}
Output: {"a":"1","b":"2","c":"3"}
⇒ json_encode() - PHP Associative Array to JSON Object
2023-08-17, ∼2664🔥, 0💬
Popular Posts:
What properties and functions are supported on http.client.HTTPResponse objects? If you get an http....
How to install .NET Framework in Visual Studio Community 2017? I have the Visual Studio Installer in...
How to include additional claims in Azure AD v2.0 id_tokens? If you want to include additional claim...
How to build a test service operation to dump everything from the "context.Request" object in the re...
How to use "{{...}}" Liquid Codes in "set-body" Policy Statement? The "{{...}}" Liquid Codes in "set...