Home > Backend Development > PHP Problem > How to add value to json in php

How to add value to json in php

青灯夜游
Release: 2023-03-16 11:22:02
Original
2929 people have browsed it

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.

<?php
header("Content-type:text/html;charset=utf-8");
$json = &#39;{"a":"php","b":"mysql","c":3}&#39;; 
var_dump($json); 
$arr=json_decode($json,TRUE);   
var_dump($arr);
?>
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template