我將派出數組到Java爲了填充一個ListView,我怎麼得到下面的代碼輸出數組?:在PHP中使用
<?php
$user = 'root';
$pass = '';
$db = 'uopuser';
$con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');
$statement = mysqli_prepare($con, 'SELECT * FROM society');
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $society_id, $name, $email, $description);
$society = array();
while(mysqli_stmt_fetch($statement)){
$society['society_id'] = $society_id;
$society['name'] = $name;
$society['email'] = $email;
$society['description'] = $description;
echo json_encode($society);
}
echo json_encode($society);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
而不是:
{"society_id":1,"name":"TestName1","email":"[email protected]","description":"TestDes1"}
{"society_id":2,"name":"TestName2","email":"[email protected]","description":"TestDes2"}
{"society_id":3,"name":"TestName3","email":"[email protected]","description":"TestDes3"}
我發表了這篇文章之前看過有關互聯網,但我很困惑!感謝任何人提前。
你可以這樣做:$ societies = array(); $ societies.push($社會);回聲json_encode($社會); 將$社會推送到循環內部的$ societies數組中,並在此循環之外進行編碼。 –