PHP Add JSON object Data Example

Today, We want to share with you PHP Add JSON object Data Example.In this post we will show you , hear for How to add element to JSON object using PHP we will give you demo and example for implement.In this post, we will learn about how to add item to the json file formatted array with an example.

PHP Add JSON object Data Example

There are the Following The simple About PHP Add JSON object Data Example Full Information With Example and source code.

Also Read This πŸ‘‰   Check Armstrong Number in GO Program

As I will cover this Post with live Working example to develop How to merge two PHP JSON array into one, so the php json foreach key value for this example is following below.

php create json object Examples

how to add item to the json file formatted array

 $str = '{ 


     } ]

 $arr = json_decode($str, true);
 $arrne['name'] = "RahulGandhi";
 array_push( $arr['members'], $arrne );

Add new data into PHP JSON string

We need to simple PHP json_decode($data) first, And then add the new data key/value, as well as json_encode() it.

$json = json_decode($data,true);

//simple to add new values
$json['mykey'] = 'myValue';

Update/edit a JSON file using PHP

//setp 1 :
$jsonString = file_get_contents('jsonDataFile.json');
$data = json_decode($jsonString, true);

//Setp 2: Then some change the data :
$data[0]['activity_name'] = "CRICKET";
foreach ($data as $key => $entry) {
    if ($entry['activity_code'] == '1') {
        $data[$key]['activity_name'] = "CRICKET";

//Step 3: re-encode it and save
$newJsonString = json_encode($data);
file_put_contents('jsonDataFile.json', $newJsonString);

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read This πŸ‘‰   Merge Multiple PHP JSON Array Merge into one array


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Add JSON object Data Example.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.