How To Insert A New Key Value To The Existing JSON- PHP
Maybe your like
- Home
- How to insert a new key value to the existing JSON- PHP
How to insert a new key value to the existing JSON- PHP
Get the solution ↓↓↓ I have been searching online for the past 2 hours and couldn't find an answer. I have some data stored in my database in JSON format. I want to add a new key and value to the json data. Example
{ "systemService":"1234", "DATA_PLAN":"SMEF", "amount":"5000" } $message = .$name "" .$post; Copy codeI want to add a new key and value to it."message":$message
to something like this.
{ "systemService":"1234", "DATA_PLAN":"SMEF", "amount":"5000" "message":"mike accountance" } Copy codeHow do I go about it in PHP
Undefined asked 2021-11-16 Write your answer 316 votes
Answer
Solution:
Here is an example how you can do it:
Method 1 decoding it to an associative array:
$object = json_decode('{ "systemService":"1234", "DATA_PLAN":"SMEF", "amount":"5000" }', true); $object['message'] = "Your message"; echo json_encode($object); //this will give you the json string with your new property Copy codeMethod 2 decoding it to an object:
$object = json_decode('{ "systemService":"1234", "DATA_PLAN":"SMEF", "amount":"5000" }'); $object->message = "Your message"; echo json_encode($object); //this will give you the json string with your new property Copy code Write your answer 684 votes
Answer
Solution:
You could convert the JSON string to object (with PHP
Tag » Add Element Json Php
-
How To Add Element To JSON Object Using PHP? - Stack Overflow
-
Php Append Data Into Existing Json Code Example
-
PHP Add JSON Object Data Example - Pakainfo
-
JSON PHP - W3Schools
-
PHP And JSON - W3Schools
-
How To Add Element To Json Object Using Php | Learn
-
Append Data To JSON File Using PHP - YouTube
-
Json_decode - Manual - PHP
-
Json_encode - Manual - PHP
-
Write And Append Data In JSON File Using PHP - C# Corner
-
How To Create An Array For JSON Using PHP? - GeeksforGeeks
-
Adding An Array To JSON Object Inside An Array - Laracasts
-
Dealing With JSON Arrays And Objects In PHP - Elastic
-
How To Add Element To JSON Object Using PHP? - Array Overflow
-
Eloquent: Serialization - The PHP Framework For Web Artisans
-
Using PHP Arrays With JSON - IBM
-
Add Element To A JSON File??
-
How To Encode And Decode JSON Data In PHP - Tutorial Republic
-
PHP Array To JSON: How To Use PHP Json_encode() - AppDividend