How can the use of arrays in PHP improve the process of converting database results to JSON?
When converting database results to JSON in PHP, using arrays can improve the process by allowing for structured data organization and easy iteration through the results. By storing the database results in an array, we can easily encode the array into JSON format using the json_encode() function. This makes it simpler to handle and manipulate the data in JSON format.
// Assume we have fetched database results and stored them in $results variable
$data = array();
foreach($results as $row) {
$data[] = $row;
}
$json = json_encode($data);
echo $json;