简体   繁体   中英

PHP Json file merge

I need to merge the json files in 1 file .

I have json like this in 3 files

{
    "count": 2077,
    "records": [
{
        "comm_date": "51529",
        "Certificate_Number": "31",
},
{
        "comm_date": "51529",
        "Certificate_Number": "31",
}
]} 

But the probel is because i have count and records arrays are here so its not mergign successfuly

<?php

$a1 = file_get_contents ("EngroClaims.json");
$a2 = file_get_contents ("EngroClaims2.json");

$a6 = file_get_contents ("SCB1Claims.json");
$a7 = file_get_contents ("TotalParcoClaims.json");
$a8 = file_get_contents ("SAPTClaims.json");

 $r = array_merge(json_decode($a1,true), json_decode($a2,true), json_decode($a6,true), json_decode($a7,true), json_decode($a8,true));

file_put_contents('conventional.json', json_encode($r));

?>

this is my php code which is working fine. But i need to merge all arrays of records

Example

First File

{
    "count": 2,
    "records": [
{
        "comm_date": "1",
},
{
        "comm_date": "2",
}
]} 

Second File

{
    "count": 3,
    "records": [
{
        "comm_date": "1",
},
{
        "comm_date": "2",
},
{
        "comm_date": "3",
}
]} 

Expected result

{
    "count": 9, //this count value is not imprtant assume it will show 1
    "records": [
{
        "comm_date": "1",
},
{
        "comm_date": "2",
},
{
        "comm_date": "1",
},
{
        "comm_date": "2",
},
{
        "comm_date": "3",
}
]} 

You can use this way;

<?php

$a1 = file_get_contents ("EngroClaims.json");
$a2 = file_get_contents ("EngroClaims2.json");


$a1 = json_decode($a1, true);
$a2 = json_decode($a2, true);
$count = $a1["count"] + $a2["count"];
$data = array();
$data["count"] = $count;
foreach ($a1["records"] as $row) {
    $data["records"][] = $row;
}
foreach ($a2["records"] as $row) {
    $data["records"][] = $row;
}
echo json_encode($data);

I have found your problem. Remove the comma in the json (explained in the code).

$a1 = '{
    "count": 2077,
    "records": [
{
        "comm_date": "51529",
        "Certificate_Number": "31", <--- THIS IS A PROBLEM
},
{
        "comm_date": "51529",
        "Certificate_Number": "31", <--- THIS IS A PROBLEM
}
]}';

/* in order for this solution to work you need to delete that comma. 
    Because there isn't a following data in the json so the comma should not be there */

Check this link; http://sandbox.onlinephpfunctions.com/code/8dfab10352cf4966c95eb599d2ed8644b24b49ab

you can use array_merge_recursive if you don't need count

$r = array_merge_recursive(json_decode($a1,true), json_decode($a2,true),json_decode($a3,true));
echo json_encode($r);

will give you the result

{"count":[3,3,3],"records":[{"comm_date":"1"},{"comm_date":"2"},{"comm_date":"3"},{"comm_date":"1"},{"comm_date":"2"},{"comm_date":"3"},{"comm_date":"1"},{"comm_date":"2"},{"comm_date":"3"}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM