亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP Json 文件合并

PHP Json 文件合并

PHP
慕勒3428872 2022-07-09 16:31:39
我需要將 json 文件合并到 1 個文件中。我在 3 個文件中有這樣的 json{    "count": 2077,    "records": [{        "comm_date": "51529",        "Certificate_Number": "31",},{        "comm_date": "51529",        "Certificate_Number": "31",}]} 但是探針是因為我有計數和記錄數組在這里所以它沒有成功合并<?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));?>這是我的 php 代碼,它工作正常。但我需要合并所有數組records例子第一個文件{    "count": 2,    "records": [{        "comm_date": "1",},{        "comm_date": "2",}]} 第二個文件{    "count": 3,    "records": [{        "comm_date": "1",},{        "comm_date": "2",},{        "comm_date": "3",}]} 預期結果{    "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",}]} 
查看完整描述

2 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

您可以使用這種方式;


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

我發現了你的問題。去掉 json 中的逗號(代碼中有解釋)。


$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 */

檢查此鏈接; http://sandbox.onlinephpfunctions.com/code/8dfab10352cf4966c95eb599d2ed8644b24b49ab


查看完整回答
反對 回復 2022-07-09
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

如果不需要計數,可以使用 array_merge_recursive

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

會給你結果

{"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"}]}


查看完整回答
反對 回復 2022-07-09
  • 2 回答
  • 0 關注
  • 270 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號