简体   繁体   中英

Merge content of two arrays together

Currently I have two seperate arrays. One for posts and one for comments. By my JSON string below you can see my comments array is in a separate array under posts. I thought I could use $JSON[] on both, but this isn't the case.

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"
}

I'm looking for something like this.

        {
        "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"
         }]
         }

COMMENTS ARRAY

   $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));

UPDATE FUNCTION

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;'>");
                }

            });
        }
    })
}

If the key are different in both array then you can it.

$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;

It's working on my case, hope it's use full for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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