Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否可以从多个json文件中提取和合并单个json对象?_Php_Json - Fatal编程技术网

Php 是否可以从多个json文件中提取和合并单个json对象?

Php 是否可以从多个json文件中提取和合并单个json对象?,php,json,Php,Json,对不起,标题很混乱。我在合并一些JSON文件时遇到了一些问题。我需要将所有产品合并到单独文件中的一个数组中 我有一个满是相同结构的json文件的目录。我使用glob选择所有文件,并将json文件解码->追加-->编码为一个大文件 这是我的密码: <?php $files = glob("*.json"); $newDataArray = []; foreach($files as $file){ $thisData = file_get_contents($file); $

对不起,标题很混乱。我在合并一些JSON文件时遇到了一些问题。我需要将所有产品合并到单独文件中的一个数组中

我有一个满是相同结构的json文件的目录。我使用glob选择所有文件,并将json文件解码->追加-->编码为一个大文件

这是我的密码:

<?php
$files = glob("*.json");
$newDataArray = [];
foreach($files as $file){
    $thisData = file_get_contents($file);
    $thisDataArray = json_decode($thisData);
    $newDataArray[] = $thisDataArray;
}
$newDataJSON = json_encode($newDataArray);
file_put_contents("merged.json",$newDataJSON);
?>    
File2.json

{
  "status": true,
  "user": {
    "username": "sally",
    "avatar": "/images/default-avatar.png",
    "products": [
      {
        "id": "35vR4hr",
        "title": "Picture 1",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "na1Id4t",
        "title": "Picture 2",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }
}
    {
  "status": true,
  "user": {
    "username": "Jessica",
    "avatar": "/images/default-avatar.png",
    "products": [
      {
        "id": "wjiefi94",
        "title": "Picture 3",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "n34idwi",
        "title": "Picture 4",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }
}
{
    "products": [
      {
        "id": "wjiefi94",
        "title": "Picture 1",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "n34idwi",
        "title": "Picture 2",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      },
            {
        "id": "n34idwi",
        "title": "Picture 3",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      },
            {
        "id": "n34idwi",
        "title": "Picture 4",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }
我希望合并数据,如下所示:

merged.json

{
  "status": true,
  "user": {
    "username": "sally",
    "avatar": "/images/default-avatar.png",
    "products": [
      {
        "id": "35vR4hr",
        "title": "Picture 1",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "na1Id4t",
        "title": "Picture 2",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }
}
    {
  "status": true,
  "user": {
    "username": "Jessica",
    "avatar": "/images/default-avatar.png",
    "products": [
      {
        "id": "wjiefi94",
        "title": "Picture 3",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "n34idwi",
        "title": "Picture 4",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }
}
{
    "products": [
      {
        "id": "wjiefi94",
        "title": "Picture 1",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 2,
        "currency": "CAD",
        "stock_warning": 1,
        "type": "service",
        "stock": 9223372036854776000
      },
      {
        "id": "n34idwi",
        "title": "Picture 2",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      },
            {
        "id": "n34idwi",
        "title": "Picture 3",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      },
            {
        "id": "n34idwi",
        "title": "Picture 4",
        "image": null,
        "quantity": {
          "min": 1,
          "max": 1
        },
        "price": 0.75,
        "currency": "CAD",
        "stock_warning": 3,
        "type": "service",
        "stock": 9223372036854776000
      }
    ]
  }

我希望这是有道理的。我觉得我在这里走到了死胡同。非常感谢您的帮助

您可以将外部工具称为“jq”吗?处理json(就像csv一样),特别是处理许多文件,不应该手动进行

顺便说一句,您的示例在产品2和3以及产品3和产品4之间没有逗号

您在第5行的新代码应该是这样读的,带有数组括号?否则,您将覆盖最后文件的内容:

$thisDataArray[] = json_decode($thisData);
你为什么要把Sally和Jessica的产品合并到同一个用户中?也许您可以将所有产品对象提取到一个文件中


与其说是答案,不如说是代码复查,希望能有所帮助;)

if(json\u last\u error()==json\u error\u NONE){$newDataArray[]=$thisDataArray;}else{echo$file.。不包含有效的json”}
。把它放在你的foreach循环中。很高兴知道我可以忽略错误。但现在它只是将读取的第一个json保存到merged.json。我想我在循环中的某个地方出错了。刚刚编辑了那个评论。犯了一个小错误。此外,您不应该忽略错误。该代码用于通知您有问题的文件。我感谢你的帮助,我用我用来测试的实际json更新了我的帖子,而不是一个例子。由于某些原因,您编辑的代码现在只发送了一个错误。我对我的JSON不够清楚,我正在尝试合并。我试图只合并产品,但在合并JSON时,它会自动这样做。我同意忽略错误是不好的做法,我想这不是我所说的忽略。对我来说太晚了一点。这就是我所能做到的:它给了你想要的结果,但还远远不够完美。不过你还是可以用它谢谢阿尔弗雷德的建议。我认为这实际上是一个好主意,我将研究如何实现这一点。可能容易多了。而且,你是对的,我在合并结果中出错了。谢谢你的提醒。至于代码:$thisDataArray[]=json_decode($thisData);我不确定,我以前试过这个,但试图简化它。我要试一试。谢谢,我忘了提一下,把两个名字合并在一起也是一个错误。我再次修复了json,以显示我正在努力实现的目标。