PHP JSON Array Merge

In this tutorial, we are going to merge a JSON array using PHP, we have three student data to be merged together. The JSON objects will be decoded into an associative array and after the encoded array merge.

php json array merge

PHP can be merging one or more JSON arrays in various ways. for example using of PHP array_merge() function.

Here, PHP JSON ARRAY Merge has two methods are works in one example: 

syntax:array_merge(array1, array2, array3, ...) 




JSON ARRAY IN PHP

  
<?php
$student1 = '{
    "id": "101",
    "student_name": "chandra",
    "class": "6",
    "status": "present"   
}';

$student2 =  '{
    "id": "102",
    "student_name": "Anbu",
    "class": "6",
    "status": "absent"
}';
$student3 ='{
	"id":"103",
	"student_name":"priya",
	"class":"6",
	"status":"present"
	
}';
$details[] = json_decode($student1,true);
$details[] = json_decode($student2,true);
$details[] = json_decode($student3,true);
$json_merge = json_encode($details);
?>
  
  



HTML:


<html>
<head>JSON_MERGE </head>
<body>
<h4>Examples:</h4> 
<p>
<div>$student1 = <?php echo $student1; ?></div>
<div>$student2 = <?php echo $student2; ?></div>
<div>$student2 = <?php echo $student3; ?></div>
</p>
<h4>Output:</h4> 
<div><?php echo $json_merge; ?></div>
<div><?php print_r(array_merge($details)); ?></div>
</body>
</html>




FINAL FULL CODE:

  
 <?php
$student1 = '{
	"id": "101",
    "student_name": "chandra",
    "class": "6",
    "status": "present"   
}';

$student2 =  '{
    "id": "102",
    "student_name": "Anbu",
    "class": "6",
    "status": "absent"
}';
$student3 ='{
	"id":"103",
	"student_name":"priya",
	"class":"6",
	"status":"present"
	
}';
$details[] = json_decode($student1,true);
$details[] = json_decode($student2,true);
$details[] = json_decode($student3,true);
$json_merge = json_encode($details);
?> 
<html>
<head>JSON_MERGE </head>
<body>
<h4>Examples:</h4> 
<p>
<div>$student1 = <?php echo $student1; ?></div>
<div>$student2 = <?php echo $student2; ?></div>
<div>$student2 = <?php echo $student3; ?></div>
</p>
<h4>Output:</h4> 
<div><?php echo $json_merge; ?></div>
<div><?php print_r(array_merge($details)); ?></div>
</body>
</html>
  
  



OUTPUT:

  
$student1 = { "id": "101", "student_name": "chandra", "class": "6", "status": "present" }
$student2 = { "id": "102", "student_name": "Anbu", "class": "6", "status": "absent" }
$student2 = { "id":"103", "student_name":"priya", "class":"6", "status":"present" }


[{"id":"101","student_name":"chandra","class":"6","status":"present"},
{"id":"102","student_name":"Anbu","class":"6","status":"absent"},
{"id":"103","student_name":"priya","class":"6","status":"present"}]


Array ( [0] => Array ( [id] => 101 [student_name] => chandra [class] => 6 [status] => present ) 
[1] => Array ( [id] => 102 [student_name] => Anbu [class] => 6 [status] => absent ) 
[2] => Array ( [id] => 103 [student_name] => priya [class] => 6 [status] => present ) )
  
  


chandrakumar

Hi, Am Chandra Kumar, I have completed my graduation in B.E computer science and Engineering. I am the founder of Dailyaspirants and I have been doing blogging and website design and development .since 2018 and 8+experience gained in this field.

*

Post a Comment (0)
Previous Post Next Post