New to php. I am trying to send JSON data to front end in name-value pair. I tried an example which I got here The following is my code fragment which sends the data in JSO
You should try pushing each object at the end of array like adding to a stack using array_push
(the equivalent of $array[] = $data
, but makes it more readable to you).
$list=array(); //instantiate the array
while($stmt->fetch()){
$data = new stdClass(); // create a new object
$data->id=$fid;
$data->name=$fname;
array_push($list,$data); // push object to stack array
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);