user3300327
user3300327

Reputation: 17

Json output shows empty array

I am using php to retrieve data from localhost wampserver. The script is below:

$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));

?>

the json output contains all the data but also includes one extra element in starting.I am not able to figure out what is that and how to remove that. the o/p is shown below:

{"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":[]--From where does this comes from?

Upvotes: 0

Views: 41

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31739

You are defining an empty array in array. Try with -

$topicarray = array();

Upvotes: 2

Related Questions