What is the issue with the array keys not taking the desired values in the PHP code provided?

The issue with the array keys not taking the desired values in the PHP code provided is that the keys are being overwritten in each iteration of the loop. To solve this issue, you can use a separate variable to keep track of the key values and increment it in each iteration.

<?php
$names = ['Alice', 'Bob', 'Charlie', 'David'];
$ages = [25, 30, 35, 40];

$result = [];
$key = 0; // Initialize key variable

foreach ($names as $name) {
    $result[$key] = ['name' => $name, 'age' => $ages[$key]];
    $key++; // Increment key value
}

print_r($result);
?>