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.

Read Also:  How to Find the Document Root using PHP Script

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

Read Also:  Merge Multiple PHP JSON Array Merge into one array

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 :

Read Also:  jQuery Parsing PHP JSON Data


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.

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  gcd of two numbers in c program
  2. Read Also:  How to Decode JSON Data in PHP?
  3. Read Also:  how to create api in php json tutorial
  4. Read Also:  Create JSON with json_encode and json_decode using PHP
  5. Read Also:  Javascript Check if Object is empty
  6. Read Also:  PHP Delete All Directory Files In Directory
  7. Read Also:  json to array php
  8. Read Also:  How to Create a Currency Converter using Pipe with Angularjs?
  9. Read Also:  PHP Get Dropdown list of all Timezones
  10. Read Also:  PHP Image pixelate Implementation pixel by pixel