How can arrays be utilized to store data retrieved from a database query in PHP and allow for flexible access to the data?

To store data retrieved from a database query in PHP and allow for flexible access to the data, arrays can be utilized. By fetching the data from the database query result and storing it in an array, we can easily access and manipulate the data in a structured manner. Associative arrays can be particularly useful for storing database query results as they allow us to access data using meaningful keys.

// Assuming $db is the database connection object
$query = "SELECT * FROM table_name";
$result = $db->query($query);

$data = array();
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Accessing the data
foreach ($data as $row) {
    echo $row['column_name'];
}