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.

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 = '{ 
  "members":[
     {

          "name":"Modi",
          "image":"/Images/Modi.jpg",
          "roll_over_image":"modi.jpg"
     },
     {

          "name":"Mehta",
          "image":"/Images/mehta.gif",
          "roll_over_image":"mehta.jpg"
     } ]
   }';


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

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

how to edit json files?

You can edit JSON files using a text editor or a specialized JSON editor. Here’s how you can edit JSON files using a text editor:

  1. Open the JSON file in a text editor: Use a text editor like Notepad, TextEdit, or Sublime Text to open the JSON file you want to edit.
  2. Make changes to the JSON file: You can edit the JSON file as you would any other text file. Make sure you follow the syntax rules of JSON, such as using double quotes around keys and string values, and separating key-value pairs with commas.
  3. Save the changes: After making the necessary changes, save the JSON file with the same filename and extension. Your changes will be saved in the file.

Alternatively, you can use a specialized JSON editor to make changes to JSON files. There are many JSON editors available, both online and offline. These editors provide a more user-friendly interface for editing JSON files and can highlight syntax errors, validate the JSON data, and provide other useful features for working with JSON.

Some examples of popular JSON editors include:

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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 Pakainfo.com 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.

Leave a Comment