To combine two arrays into a single associative array in PHP, you can use the array_combine()
function. This function takes one array as keys and another array as values and combines them into a new associative array.
Here's an example:
// Array 1
$keys = ['name', 'age', 'city'];
// Array 2
$values = ['John Doe', 25, 'New York'];
// Combine arrays into a single associative array
$combinedArray = array_combine($keys, $values);
// Print the combined array
print_r($combinedArray);
Output:
Array
(
[name] => John Doe
[age] => 25
[city] => New York
)
In this example, the array_combine()
function takes the $keys
array as keys and the $values
array as values, then returns an associative array with the keys and values combined.