How can one dynamically handle varying column numbers in a MySQL row when converting it into an associative array in PHP?

When converting a MySQL row into an associative array in PHP, the issue arises when the number of columns in the row varies. To dynamically handle this, you can fetch the column names from the result set and then loop through each column to create the associative array.

// Assume $row is the MySQL row fetched as an associative array
$assocArray = array();

// Loop through each column in the row
foreach ($row as $key => $value) {
    $assocArray[$key] = $value;
}

// Now $assocArray contains the MySQL row as an associative array