How can associative arrays with matching keys be multiplied in PHP?

When multiplying associative arrays with matching keys in PHP, you can loop through the arrays, multiply the values with the same keys, and store the result in a new array. This can be achieved by using a foreach loop to iterate over one of the arrays and checking if the key exists in the other array before performing the multiplication.

$array1 = ['a' => 2, 'b' => 3, 'c' => 4];
$array2 = ['a' => 5, 'b' => 2, 'c' => 1];
$result = [];

foreach ($array1 as $key => $value) {
    if (array_key_exists($key, $array2)) {
        $result[$key] = $value * $array2[$key];
    }
}

print_r($result);