donglei2022 2019-06-24 00:09
浏览 74
已采纳

PHP如何将另一个文件中的Object添加到不同JSON的数组中

I want to move this object (in PHP)

(json1.json)

{
    "id": "TEST",
    "name": "TEST",
    "desc": "TEST",
    "image": "TEST",
    "package": "TEST",
    "version": "1.00",
    "picpath": "TEST",
    "desc_1": "ssss",
    "desc_2": "ssss",
    "ReviewStars": "5",
    "Size": "TEST",
    "Author": "TEST",
    "apptype": "222",
    "pv": "TEST",
    "main_icon_path": "TEST",
    "main_menu_pic": "TEST",
    "releaseddate": "2019-06-19",
}

into this array (json2.json)

{
  "packages": [
  {
      "id": "TEST",
      "name": "TEST",
      "desc": "TEST",
      "image": "TEST",
      "package": "TEST",
      "version": "1.00",
      "picpath": "TEST",
      "desc_1": "TEST",
      "desc_2": "TEST",
      "ReviewStars": "TEST",
      "Size": "",
      "Author": "TEST",
      "apptype": "TEST",
      "pv": "TEST",
      "main_icon_path": "TEST",
      "main_menu_pic": "TEST",
      "releaseddate": "5/03/2018"
    }
    ]
}

i want the final json to look like this

{
  "packages": [
  {
      "id": "TEST",
      "name": "TEST",
      "desc": "TEST",
      "image": "TEST",
      "package": "TEST",
      "version": "1.00",
      "picpath": "TEST",
      "desc_1": "TEST",
      "desc_2": "TEST",
      "ReviewStars": "TEST",
      "Size": "",
      "Author": "TEST",
      "apptype": "TEST",
      "pv": "TEST",
      "main_icon_path": "TEST",
      "main_menu_pic": "TEST",
      "releaseddate": "5/03/2018"
    },
{
    "id": "TEST",
    "name": "TEST",
    "desc": "TEST",
    "image": "TEST",
    "package": "TEST",
    "version": "1.00",
    "picpath": "TEST",
    "desc_1": "ssss",
    "desc_2": "ssss",
    "ReviewStars": "5",
    "Size": "TEST",
    "Author": "TEST",
    "apptype": "222",
    "pv": "TEST",
    "main_icon_path": "TEST",
    "main_menu_pic": "TEST",
    "releaseddate": "2019-06-19",
}

    ]
}

iv tried many solutions including this one


$datas[] = json_decode($json, true);

$datas[] = json_decode($json1, true);

$jsonData = json_encode($datas, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

file_put_contents($jpath, $jsonData);```
  • 写回答

1条回答 默认 最新

  • dongzhong5833 2019-06-24 00:46
    关注
    <?php
    
    $json1_str = '{
        "id": "TEST",
        "name": "TEST",
        "desc": "TEST",
        "image": "TEST",
        "package": "TEST",
        "version": "1.00",
        "picpath": "TEST",
        "desc_1": "ssss",
        "desc_2": "ssss",
        "ReviewStars": "5",
        "Size": "TEST",
        "Author": "TEST",
        "apptype": "222",
        "pv": "TEST",
        "main_icon_path": "TEST",
        "main_menu_pic": "TEST",
        "releaseddate": "2019-06-19"
    }';
    
    $json2_str = '{
      "packages": [
      {
          "id": "TEST",
          "name": "TEST",
          "desc": "TEST",
          "image": "TEST",
          "package": "TEST",
          "version": "1.00",
          "picpath": "TEST",
          "desc_1": "TEST",
          "desc_2": "TEST",
          "ReviewStars": "TEST",
          "Size": "",
          "Author": "TEST",
          "apptype": "TEST",
          "pv": "TEST",
          "main_icon_path": "TEST",
          "main_menu_pic": "TEST",
          "releaseddate": "5/03/2018"
        }
        ]
    }';
    
    $json1 = json_decode($json1_str, true);
    $json2 = json_decode($json2_str, true);
    
    $json2['packages'][] = $json1;
    
    $jsonData = json_encode($json2, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    
    echo $jsonData;
    

    OUTPUT

    {
        "packages": [
            {
                "id": "TEST",
                "name": "TEST",
                "desc": "TEST",
                "image": "TEST",
                "package": "TEST",
                "version": "1.00",
                "picpath": "TEST",
                "desc_1": "TEST",
                "desc_2": "TEST",
                "ReviewStars": "TEST",
                "Size": "",
                "Author": "TEST",
                "apptype": "TEST",
                "pv": "TEST",
                "main_icon_path": "TEST",
                "main_menu_pic": "TEST",
                "releaseddate": "5/03/2018"
            },
            {
                "id": "TEST",
                "name": "TEST",
                "desc": "TEST",
                "image": "TEST",
                "package": "TEST",
                "version": "1.00",
                "picpath": "TEST",
                "desc_1": "ssss",
                "desc_2": "ssss",
                "ReviewStars": "5",
                "Size": "TEST",
                "Author": "TEST",
                "apptype": "222",
                "pv": "TEST",
                "main_icon_path": "TEST",
                "main_menu_pic": "TEST",
                "releaseddate": "2019-06-19"
            }
        ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低