我的嘗試是使一個分頁和這就是我的Ajax請求頁面:添加兩個對象到一個json_encode在PHP
while ($file = $query->fetch(PDO::FETCH_ASSOC)) {
$arrayFetch[] = [
'id' => $file['id'],
'name' => $file['name']
];
$lastId = $file['lastId'];
}
// For simplify: Here is another query code for get the the result of $count
$count = $query->rowCount();
所以,我現在有變數$arrayFetch
和$count
。
UPDATE:
我實際上是json_encode代碼:
echo '{"user":'.json_encode($arrayFetch).'}';
輸出是:
{"user":[{ "id":1,"name":"toby"},....]}
我要的是增加一個第二個對象是這樣的:
{"user":[{ "id":1,"name":"toby"},....]},
{"pagination":[{ "count":10" }]} // THIS
那不工作,這只是一個例子來說明什麼,我試圖存檔:
echo '{"user":'.json_encode($arrayFetch).'},{ "pagination": [{ "count" : '.$count.' }] ';
這不是JSON。 –
這很好,但明白沒有方括號的JSON數組不是JSON。 –