dsgwoh7038 2015-06-26 08:02
浏览 28
已采纳

使用PHP在指定位置合并2个JSON

I have 2 JSON array from two mysql tables ie, db_events and db_ads.

Inside JSON values from db_events, I want to add first JSON value from db_ads at position 5 and second value at position 10 respectively.

How can I implement this. Is this is possible.

JSON from table db_events is,

{
  "events_from_category": [
    {
      "id": "1",
      "name": "Demo Event"
    },
    {
      "id": "2",
      "name": "Demo"
    },
    {
      "id": "3",
      "name": "Event"
    },
    {
      "id": "4",
      "name": "fgvnjhfrjht"
    },
    {
      "id": "5",
      "name": "fgvnjhfrjht"
    },
    {
      "id": "6",
      "name": "fgvnjhfrjht"
    },
    {
      "id": "7",
      "name": "fgvnjhfrjht"
    },
    {
      "id": "8",
      "name": "fgvnjhfrjht"
    },
   {
      "id": "9",
      "name": "fgvnjhfrjht"
   },
   {
      "id": "10",
      "name": "fgvnjhfrjht"
   }
  ]
}

JSON from table db_ads is,

{
  "ads": [
    {
      "id": "1",
      "ads_name": "ads 1"
    },
    {
      "id": "2",
      "ads_name": "ads 2"
    }
  ]
}

My resulting JSON should be,

{
  "ads": [
    {
      "id": "1",
      "name": "Demo Event"
    },
    {
      "id": "2",
      "name": "Demo"
    },
    {
       "id": "3",
       "name": "Event"
    },
    {
       "id": "4",
       "name": "fgvnjhfrjht"
    },
    {
       "id": "1",
       "ads_name": "ads 1"
    },
    {
       "id": "6",
       "name": "fgvnjhfrjht"
    },
    {
       "id": "7",
       "name": "fgvnjhfrjht"
    },
    {
       "id": "8",
       "name": "fgvnjhfrjht"
    },
    {
       "id": "9",
       "name": "fgvnjhfrjht"
    },
    {
       "id": "2",
       "ads_name": "ads 2"
    }
  ]
}
  • 写回答

1条回答 默认 最新

  • douyuan4697 2015-06-26 08:15
    关注

    The first thing to note is that json can be decoded in to an array using:

    $aryRecords = json_decode($strRecords);
    $aryAds = json_decode($strAds);
    

    Then, because you are not just appending, it will be necessary to start looping through the array to insert the ads at certain points:

    $aryTemp = array();
    $intCount = 0;
    /* Make sure pointer at the start of the ads array */
    reset($aryAds);
    
    /* Start looping through your results */
    foreach($aryRecords as $aryCurrentRecord) {
        $intCount++;
    
        /* Check if counter divisible by 5 */
        if($intCount %5 != 0) {
            $aryTemp[] = current($aryAds);
            /* Move the pointer in the ads array forward */
            next($aryAds);
        }
    
        $aryTemp[] = $aryCurrentRecord;
    }
    
    /* Recreate your JSON object */
    $strOutput = json_encode($aryTemp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计