Home>Article>Backend Development> How to add value to json in php

How to add value to json in php

青灯夜游
青灯夜游 Original
2022-06-13 15:48:04 2782browse

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)".

How to add value to json in php

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.

How to add value to json in php

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);

How to add value to json in php

3. Use the json_encode() function to convert the array back to JSON type

$Newjson=json_encode($arr); var_dump($Newjson);

How to add value to json in php

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!

Statement:
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