PHP Add JSON object Data Example

Laravel Mysql Mysqli php Programming Technology

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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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 = '{ 


     } ]

 $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 :


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.   How to install localhost/codeigniter/ with XAMPP
  2.   PHP and Ajax Asynchronous File Upload using jQuery
  3.   PHP Fetch all HTTP request headers
  4.   Merge Multiple PHP JSON Array Merge into one array
  5.   jQuery Parsing PHP JSON Data
  6.   Creating PDF Files using PHP FPDF library - Convert HTML to PDF
  7.   Create dynamic calendar using jQuery, Ajax and PHP
  8.   Create JSON with json_encode and json_decode using PHP
  9.   Top 10 Common PHP Job Interview Questions and Answers
  10.   JavaScript Objects - methods array constructor and new object

Leave a Reply

Your email address will not be published. Required fields are marked *