在PHP中合并两个具有公共字段值的json文件

时间:2019-04-24 16:50:12

标签: php json

我已经用PHP生成了两个json文件。一,输出如下。 / json1.json

{
   "Zipcode":"22581",
   "City":"",
   "Primary State":"Maryland",
   "Common Field":"49969",
   "County":"Something"
}

第二个输出如下。 / json2.json

{
    "Common Field":"49969",
    "Option 1":"Y",
    "Option 2":"",
    "Option 3":""
}

我想找到一种方法来合并它们,最好使用PHP,这样它们的输出如下所示:

{
    "Zipcode":"22581",
    "City":"",
    "Primary State":"Maryland",
    "Common Field": {
       "Common Field":"49969",
       "Option 1":"Y",
       "Option 2":"",
       "Option 3":""
    },
    "County":"Something"
},

更新,以下推荐给两个发布者的方法不起作用:不管是否在最后使用true,这只会输出每个条目旁边带有数字的json。并且不会按上述期望的格式按公共字段连接两个json文件。


$json1 = json_decode(file_get_contents('json1.json'),true);
$json2 = json_decode(file_get_contents('json2.json'),true);

$json1['Common Field'] = $json2;

echo json_encode($json1, JSON_PRETTY_PRINT);

这只是将/ json1.json中的每个条目输出为:

"0": { "Zipcode": "20101", "City": "", "Primary State": "Virginia", "Common Field": "49530", "County": "Loudoun" }

4 个答案:

答案 0 :(得分:2)

解码数组,并使第一个与公共字段相关联。
然后循环第二个数组,并将子数组添加到第一个数组。

如果结构相同,它将比您的示例在更大的数组上工作。

$json1 = '[{
   "Zipcode":"22581",
   "City":"",
   "Primary State":"Maryland",
   "Common Field":"49969",
   "County":"Something"
},{
   "Zipcode":"11111",
   "City":"",
   "Primary State":"Maryland",
   "Common Field":"11111",
   "County":"Something"
}]';

$json2 = '[{
    "Common Field":"49969",
    "Option 1":"Y",
    "Option 2":"",
    "Option 3":""
},{
    "Common Field":"11111",
    "Option 1":"Y",
    "Option 2":"",
    "Option 3":""
}]';

$arr1 = json_decode($json1, true);
$arr2 = json_decode($json2, true);

$arr1 = array_column($arr1, null, "Common Field");

foreach($arr2 as $field){
    $arr1[$field["Common Field"]]["Common Field"] = $field;
}

var_dump($arr1);

(可选)您可以使用array_values再次将键重置为索引类型。

示例输出:

array(2) {
  [49969]=>
  array(5) {
    ["Zipcode"]=>
    string(5) "22581"
    ["City"]=>
    string(0) ""
    ["Primary State"]=>
    string(8) "Maryland"
    ["Common Field"]=>
    array(4) {
      ["Common Field"]=>
      string(5) "49969"
      ["Option 1"]=>
      string(1) "Y"
      ["Option 2"]=>
      string(0) ""
      ["Option 3"]=>
      string(0) ""
    }
    ["County"]=>
    string(9) "Something"
  }
  [11111]=>
  array(5) {
    ["Zipcode"]=>
    string(5) "11111"
    ["City"]=>
    string(0) ""
    ["Primary State"]=>
    string(8) "Maryland"
    ["Common Field"]=>
    array(4) {
      ["Common Field"]=>
      string(5) "11111"
      ["Option 1"]=>
      string(1) "Y"
      ["Option 2"]=>
      string(0) ""
      ["Option 3"]=>
      string(0) ""
    }
    ["County"]=>
    string(9) "Something"
  }
}

https://3v4l.org/FQFBq

答案 1 :(得分:0)

尝试一下:

$json1 = (array)json_decode(file_get_contents('json1.json'));
$json2 = (array)json_decode(file_get_contents('json2.json'));

$json1['Common Field'] = $json2;

echo json_encode($json1, JSON_PRETTY_PRINT);

输出:

{
    "Zipcode": "22581",
    "City": "",
    "Primary State": "Maryland",
    "Common Field": {
        "Common Field": "49969",
        "Option 1": "Y",
        "Option 2": "",
        "Option 3": ""
    },
    "County": "Something"
}

答案 2 :(得分:0)

$json1 = json_decode(file_get_contents('json1.json'),true);
$json2 = json_decode(file_get_contents('json2.json'),true);

$json1['Common Field'] = $json2;

echo json_encode($json1, JSON_PRETTY_PRINT);

当您将第二个参数设置为true时,json转换为数组,但是将false设置为默认值,则它将转换为对象。

答案 3 :(得分:0)

您可以使用<div class="wrap"> <iframe class="frame" [srcdoc]="newsletterSrcDoc"></iframe> </div>获得所需的结果

array_walk