Php 将两个数组的内容合并在一起
目前我有两个独立的阵列。一个用于帖子,一个用于评论。通过下面我的JSON字符串,您可以看到我的注释数组位于posts下的单独数组中。我想我可以在两者上都使用$JSON[],但事实并非如此 当前返回的JSONPhp 将两个数组的内容合并在一起,php,arrays,json,Php,Arrays,Json,目前我有两个独立的阵列。一个用于帖子,一个用于评论。通过下面我的JSON字符串,您可以看到我的注释数组位于posts下的单独数组中。我想我可以在两者上都使用$JSON[],但事实并非如此 当前返回的JSON - { "posts": [{ "comment_id": "2072", "comment_content": "66666", "comment_creator_last": "Ward", "comm
- {
"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"
}
评论数组
$json[]= array(
'comment_id' => $rowcomments['comment_id'],
'comment_content' => $rowcomments['comment_content'],
'comment_creator_last' => $rowcommentcreator['last'],
'comment_streamitem' => $rowcomments['comment_streamitem']
);
$json[] = array(
'streamitem_target_fullname' => $rowstarget['fullname'],
'streamitem_target_username' => $rowstarget['username'],
'streamitem_target_first' => $rowstarget['first'],
'streamitem_id' => $row['streamitem_id']
);
帖子数组
$json[]= array(
'comment_id' => $rowcomments['comment_id'],
'comment_content' => $rowcomments['comment_content'],
'comment_creator_last' => $rowcommentcreator['last'],
'comment_streamitem' => $rowcomments['comment_streamitem']
);
$json[] = array(
'streamitem_target_fullname' => $rowstarget['fullname'],
'streamitem_target_username' => $rowstarget['username'],
'streamitem_target_first' => $rowstarget['first'],
'streamitem_id' => $row['streamitem_id']
);
JSON编码
echo json_encode(array('posts' => $json));
更新功能
函数loadAll(){
$(窗口).data('ajaxready',false);
$.ajax({
url:'commentedoncontent.php',
键入:“POST”,
数据类型:“JSON”,
cache:false,
数据:{last_id:last_id},
成功:功能(数据){
$(窗口).data('ajaxready',true);
last\u id=data.last\u id
如果(!!数据){
$(“#nomoreposts”).show();
$(“#loadmoreajaxloader”).hide();
}
$.each(data.posts、function(i、response){
如果(response.streamitem_type_id==1&&response.stopcomments==1&&response.streamitem_id共享>0){
$(“#宅基地”)。追加(“”);
}
});
}
})
}
如果两个数组中的键不同,则可以使用它
我的案子正在处理,希望你能用得满
您可以说,
$result=array\u merge($json[0],$json[1])
之后,您将进入合并数组。@AmrAly很有趣,您像我一样发布了$result=array\u merge($json$json)代码>如果是这样的话。我很接近,我如何通过$从客户端获取数据。每个(data.posts,function(I,response){
如果我弄错了,很抱歉,但这是我所理解的。客户端-var a=[].concat(json[0],json[1]);
其中json
是您的json数组。
$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;