我已经在PHP中创建了两个函数,这两个函数都返回一个数组,我想合并这个数组并返回一个JSON。PHP合并2个数组然后解析它到json
示例代码:
function get_data($conn, $post_id) {
$qry = "SELECT * FROM post WHERE Post_id='".$post_id."'";
$res = mysqli_query($conn, $qry);
global $myArray;
if (mysqli_num_rows($res) > 0)
{ while($row = mysqli_fetch_assoc($res)) {
$Post_id = $row['Post_id'];
$Post_title = $row['Post_title'];
$Post_body = $row['Post_body'];
$myArray[] = array('Post_id' => $Post_id, 'Post_title' => $Post_title, 'Post_body' => $Post_body);
}
likedislike($conn, $post_id, $myArray); }
}
function likedislike($conn, $post_id, $myArray)
{
$qry1 = "SELECT * FROM likeunlike WHERE postid = '".$post_id."' AND likes=1";
$res1 = mysqli_query($conn, $qry1);
$count = mysqli_num_rows($res1);
global $myArray;
global $username;
$userlist = array();
while($row = mysqli_fetch_assoc($res1)) {
$Usrname = $row['username'];
$userlist[] = array('username' => $Usrname);
}
$newarray = array_merge($myArray, $userlist);
echo json_encode($newarray);
}
显示的代码。你有什么尝试。提示:阅读['array_merge'](http://php.net/manual/en/function.array-merge.php),了解它的行为与'$ mergedArray = $ array1 + $ array;' – ficuscr
没有问题,如果它包含键和值,,,它的存储在变量中。 –
检查此... https://eval.in/817282 ...例如..如果其包含键和值.. –