我已經在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 ...例如..如果其包含鍵和值.. –