0

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?

1
  • 1
    $topicarray[] = array(); try removing [] after $topicarray Commented Sep 7, 2015 at 8:00

1 Answer 1

2

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

$topicarray = array();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.