How can a 2-dimensional array be helpful in storing values retrieved from SQL queries in PHP?

When retrieving values from SQL queries in PHP, a 2-dimensional array can be helpful in storing the results in a structured format. This allows for easy access to the data by using row and column indexes. By storing the SQL query results in a 2-dimensional array, you can iterate through the rows and columns to manipulate or display the data as needed.

// Assuming you have already connected to your database and executed a SQL query

// Fetch all rows from the SQL query result and store them in a 2-dimensional array
$data = array();
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Accessing the data in the 2-dimensional array
foreach ($data as $row) {
    echo $row['column_name']; // Accessing data by column name
}