1

我想添加,比如说,某种标题。我的 PHP 返回如下内容:

[{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"matematika"}]

我想得到这样的东西

{"lista"[{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"matematika"}]}

这是我的 PHP

$con = mysqli_connect($host, $user, $pwd, $db);

if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 


$sql = "SELECT Grad, Predmet FROM lista";
$result = mysqli_query($con, $sql);


$rows = array();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {


        $rows[] = $row; 

}

mysqli_close($con);

echo json_encode($rows);
4

2 回答 2

3
$rows = (object) array('lista' => $rows);
echo json_encode($rows);
于 2013-07-29T11:40:43.450 回答
0

像这样的东西: $rows['lista'][] = $row ?

于 2013-07-29T11:41:44.803 回答