How can keys be dynamically assigned in a new array based on database query results in PHP?

When fetching data from a database in PHP, you can dynamically assign keys in a new array based on the query results by looping through the rows and assigning values to specific keys. This can be achieved by using the fetch_assoc() method to retrieve associative arrays from the query results, where the keys represent column names.

// Assuming $result contains the query results
$newArray = array();

while ($row = $result->fetch_assoc()) {
    $newArray[$row['key_name']] = $row['value'];
}

print_r($newArray);