How can associative arrays be used to store values from a database in PHP?

Associative arrays can be used to store values from a database in PHP by fetching the data from the database and assigning it to keys in the array. Each row from the database can be stored as an associative array where the column names are used as keys and the corresponding values are stored as values. This allows for easy access and manipulation of the database values in PHP.

// Connect to the database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Query to fetch data from the database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Initialize an empty array to store the database values
$data = array();

// Fetch each row from the result set and store it as an associative array in $data
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Access and manipulate the database values stored in the associative array
foreach ($data as $row) {
    echo $row['column_name'] . "<br>";
}

// Close the database connection
mysqli_close($connection);