How can two arrays be linked or combined in PHP to retrieve corresponding values efficiently?
To link or combine two arrays in PHP to retrieve corresponding values efficiently, you can use the array_combine() function. This function creates an array by using one array for keys and another for its values. This allows you to easily retrieve corresponding values based on the keys from the first array.
// Two arrays to be linked
$keys = ['name', 'age', 'city'];
$values = ['John', 30, 'New York'];
// Combine the arrays to link keys with values
$combinedArray = array_combine($keys, $values);
// Retrieve corresponding values efficiently
echo $combinedArray['name']; // Output: John
echo $combinedArray['age']; // Output: 30
echo $combinedArray['city']; // Output: New York