将两个数组的内容合并在一起

时间:2017-03-07 01:24:19

标签: php arrays json

目前我有两个单独的数组。一个用于帖子,一个用于评论。通过下面的JSON字符串,您可以看到我的注释数组位于帖子下的单独数组中。我以为我可以在两者上都使用$ JSON [],但事实并非如此。

CURRENT RETURNED JSON

    -    {
    "posts": [{
        "comment_id": "2072",
        "comment_content": "66666",
        "comment_creator_last": "Ward",
        "comment_streamitem": "1547"
    }, {
        "commentlinktoggle": "<div id='streamcomment' style='cursor:pointer;display:block;' class='toggleon1547'><a style='cursor:pointer;' id='commenttoggle_1547' onclick=\"toggle_comments('comment_holder_1547');swapcommentlabel(this.id);\"><div id='loadcommentcount1547'>Comments 1<\/div><\/a><\/div>",
        "streamitem_formholder": "<form style=\"display:block;\" class=\"mycommentform\" id=\"mycommentform1547\" method=\"POST\">\r\n<input type=\"hidden\"  name=\"tocommentuserid\" id=\"tocommentuserid\" value=\"34\">\r\n<input type=\"hidden\"  name=\"fromcommentuserid\" id=\"fromcommentuserid\" value=\"34\">\r\n<input type=\"hidden\"  name=\"fullname\" id=\"fullname\" value=\"Dave Keith Ward\">\r\n<input type=\"hidden\" name=\"streamidcontent\" id=\"streamidcontent\" value=\"1547\">\r\n<textarea type=\"input\" cols=\"40\" rows=\"5\" name=\"commentingcontents\" id=\"commentingcontents\" placeholder=\"Say something\"><\/textarea><br \/>\r\n<input type=\"submit\" id=\"button\" value=\"Comment\"><\/form><br \/><hr><br \/>",
        "stopcommentsbutton": "<div id='Showcommentsid1547'>\r\n<button id='Hidecomments' title='Turn Off Comments' cursor='pointer' onClick=\"Hidecommentsaj(1,1547);\"> On<\/button><\/div>",
        "streamitem_uploadimage_count": "",
        "streamitem_imageuploaded": [],
        "streamitem_content": "uuu",
        "streamitem_type_id": "1",
        "streamitem_timestamp": "<div id='time' title='Posted on Sunday  08 Jan 2017 at 06:17PM '>1 month <\/div>",
        "streamitem_collect": "<li><a id=\"collectpostlink1547\" style=\"cursor:pointer;color:#000;\" onclick=\"collectpost(1547);\">Collect<\/a><\/li>",
        "streamitem_image_creator": "userimages\/cropped34.jpg?1488854045",
        "streamitem_image_target": "userimages\/cropped34.jpg?1488854045",
        "streamitem_photo_id": "0",
        "stopcomments": "1",
        "public": "Public",
        "streamitem_idshared": "0",
        "streamitem_collecttype": "0",
        "sharedcontent_creatorid": "0",
        "sharedcontent_usercontent": "",
        "sharedcontent_userstimestamp": "<div id='time' title='Posted on Thursday  01 Jan 1970 at 01:00AM '>2017 years <\/div>",
        "video_data": "",
        "video_image": "",
        "video_caption": "",
        "streamitem_target_fullname": "Dave Keith Ward",
        "streamitem_target_username": "developerdave",
        "streamitem_target_first": "Dave",
        "streamitem_target_middle": "Keith",
        "streamitem_target_last": "Ward",
        "streamitem_target_id": "34",
        "streamitem_creator_username": "developerdave",
        "streamitem_creator_fullname": "Dave Keith Ward",
        "streamitem_creator_first": "Dave",
        "streamitem_creator_middle": "Keith",
        "streamitem_creator_last": "Ward",
        "streamitem_creator_id": "34",
        "streamitem_id": "1547"
    }, 
    }],
    "last_id": "2065"
}

我正在寻找类似的东西。

        {
        "posts": [{
            "comment_id": "2072",
            "comment_content": "66666",
            "comment_creator_last": "Ward",
            "comment_streamitem": "1547",
            "streamitem_target_fullname": "Dave Keith Ward",
            "streamitem_target_username": "developerdave",
            "streamitem_target_first": "Dave",
            "streamitem_id": "1547"
         }]
         }

评论阵列

   $json[]= array(
   'comment_id' => $rowcomments['comment_id'],
   'comment_content' => $rowcomments['comment_content'],
   'comment_creator_last' => $rowcommentcreator['last'],
   'comment_streamitem' => $rowcomments['comment_streamitem']
    );

POSTS ARRAY

    $json[] = array(
    'streamitem_target_fullname' => $rowstarget['fullname'],
    'streamitem_target_username' => $rowstarget['username'],
    'streamitem_target_first' => $rowstarget['first'],
    'streamitem_id' => $row['streamitem_id']
      );

JSON ENCODE

    echo json_encode(array('posts' => $json));

更新 功能

function loadAll () {
    $(window).data('ajaxready', false);

    $.ajax({
        url: 'commentedoncontent.php',
        type: 'POST',
        dataType: 'JSON',
        cache:false,
        data: { last_id: last_id},
        success: function(data) {
            $(window).data('ajaxready', true);
            last_id = data.last_id

            if (!!data) {
                $("#nomoreposts").show();
                $("#loadmoreajaxloader").hide();
            }

            $.each(data.posts, function(i, response) {

                if (response.streamitem_type_id == 1 && response.stopcomments == 1 && response.streamitem_idshared>0) { 
                    $("#homestatusid").append("<div id='fade"+response['streamitem_id']+"' style='display:none;' class='black_overlay'></div><div id='deleteconfirm"+response['streamitem_id']+"' class='deleteconfirm' style='display:none;'>");
                }

            });
        }
    })
}

1 个答案:

答案 0 :(得分:0)

  

如果两个阵列中的键都不同,那么就可以。

$json1 = array(
   'comment_id' => $rowcomments['comment_id'],
   'comment_content' => $rowcomments['comment_content'],
   'comment_creator_last' => $rowcommentcreator['last'],
   'comment_streamitem' => $rowcomments['comment_streamitem']
    );

$json1 = array(
    'streamitem_target_fullname' => $rowstarget['fullname'],
    'streamitem_target_username' => $rowstarget['username'],
    'streamitem_target_first' => $rowstarget['first'],
    'streamitem_id' => $row['streamitem_id']
      );

$json = $json1 + $json2;
  

它正在研究我的案子,希望它能为你充分利用。