繁体   English   中英

在 PHP 中加入多个 json 文件的最有效方法

[英]Most efficient way to join multiple json file in PHP

我正在寻找一种有效的解决方案,将多个 json arrays 合并为一个,删除重复项。 我有多个具有这种结构的 json 文件:

[
    {
        "@_fa": "true",
        "link": [],
        "prism:url": "",
        "dc:identifier": "ID-X",
        "eid": "ID-X",
        "dc:title": "",
        "dc:creator": "",
        "prism:publicationName": "",
        "prism:issn": "",
        "prism:eIssn": "",
        "prism:volume": "",
        "prism:issueIdentifier": "",
        "prism:pageRange": "",
        "prism:coverDate": "2020-05-01",
        "prism:coverDisplayDate": "",
        "prism:doi": "",
        "dc:description": "",
        "citedby-count": "0",
        "affiliation": [],
        "prism:aggregationType": "Jounal",
        "subtype": "ar",
        "subtypeDescription": "",
        "author-count": {
            "@limit": "100",
            "@total": "5",
            "$": "5"
        },
        "author": [],
        "article-number": "",
        "source-id": "",
        "fund-no": "",
        "fund-sponsor": "",
        "openaccess": "0",
        "openaccessFlag": false
    },
   {
       ...
   }
]

我以这种方式阅读这些文件:

$json_1 = json_decode(file_get_contents('file_1.json'));
$json_2 = json_decode(file_get_contents('file_2.json'));
$json_3 = json_decode(file_get_contents('file_3.json'));

此 arrays 可以具有相同的元素,这些元素可通过一些唯一字段(例如dc:identifiereiddc:title两者都是唯一的)来识别。

我想将所有$json_变量合并为一个,只保留一次不同数组之间的公共元素。 什么是最好和最有效的解决方案?

如果我正确理解您的问题,array_merge(array1,array2,....,arrayN) 应该可以工作。

$json_1 = json_decode(file_get_contents('file_1.json'));
$json_2 = json_decode(file_get_contents('file_2.json'));
$json_3 = json_decode(file_get_contents('file_3.json'));
$result = array_merge($json_1,$json_2,$json_3);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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