How to add value to json in php: 1. Use json_decode() function to convert json data into array type, syntax "json_decode(json data, TRUE)"; 2. Use "$array variable name[' New key name '] = new value;" statement adds a new value at the end of the array; 3. Use the json_encode() function to convert the added value array back to the JSON type, with the syntax "json_encode (array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
How to give json to php Add value
In php, you can use arrays to add values to json.
Implementation steps:
1. Use json_decode() function to convert json into an array
json_decode() function can decode JSON data. Convert JSON data to array type.
<?php header("Content-type:text/html;charset=utf-8"); $json = '{"a":"php","b":"mysql","c":3}'; var_dump($json); $arr=json_decode($json,TRUE); var_dump($arr); ?>
2. Use the $array variable name['new key name']=new value;
statement to add a new value to the array
$Array variable name['new key name']=new value;
statement can insert a new key-value pair at the end of the array.
$arr["d"]="hello"; var_dump($arr);
3. Use the json_encode() function to convert the array back to JSON type
$Newjson=json_encode($arr); var_dump($Newjson);
Recommended learning: "PHP video tutorial"
The above is the detailed content of How to add value to json in php. For more information, please follow other related articles on the PHP Chinese website!