How can developers effectively handle multidimensional arrays in PHP when retrieving data from a database query?
When retrieving data from a database query in PHP, developers can effectively handle multidimensional arrays by using nested loops to iterate through the results. By organizing the data into a multidimensional array, developers can easily access and manipulate the data in a structured way.
// Assume $result is the result of a database query
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
// Accessing the multidimensional array
foreach ($data as $row) {
foreach ($row as $key => $value) {
echo $key . ': ' . $value . '<br>';
}
}