在PHP中添加标题

时间:2013-07-29 11:39:38

标签: php

我想补充一下,比方说,某种标题。我的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);

2 个答案:

答案 0 :(得分:3)

$rows = (object) array('lista' => $rows);
echo json_encode($rows);

答案 1 :(得分:0)

这样的事情:$ rows ['lista'] [] = $ row?