Json输出显示空数组

时间:2015-09-07 07:58:45

标签: php json

我使用php从localhost wampserver检索数据。脚本如下:     

$sql = "select * from topic";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$topicarray[] = array();
while($row =mysqli_fetch_assoc($result))
{
    $topicarray['topic'][] = $row;
}

print(json_encode($topicarray));

?>

json输出包含所有数据,但在启动时还包含一个额外的元素。我无法弄清楚它是什么以及如何删除它。 o / p如下所示:

{"0":[],"topic":[{"Topic_id":"1","Subject":"Computer Science","Details":"Consist of various subjects","Parent_id":null},{"Topic_id":"2","Subject":"Electronics","Details":"Subjects related to electronics","Parent_id":null},{"Topic_id":"3","Subject":"databases","Details":"Will talk about sql","Parent_id":"1"},{"Topic_id":"4","Subject":"languages","Details":"c and java","Parent_id":"1"}]}

" 0":[] - 这是从哪里来的?

1 个答案:

答案 0 :(得分:2)

您正在数组中定义一个空数组。试试 -

$topicarray = array();