The `array_combine()` function in PHP is used to create an array by using one array for keys and another for its values. It returns `false` if the number of elements in the keys array does not match the number of elements in the values array.
Syntax
<?php array_combine(array $keys, array $values): array|false ?>$keys: An array of keys. $values: An array of values. Example Here's an example of how to use the `array_combine()` function in PHP 7.4:
<?php
$keys = ["apple", "banana", "cherry"];
$values = [1, 2, 3];
$result = array_combine($keys, $values);
if ($result !== false) {
print_r($result);
} else {
echo "The number of elements in both arrays do not match.";
}
?>
Output
<?php Array ( [apple] => 1 [banana] => 2 [cherry] => 3 ) ?>Explanation 1. In this example, we have two arrays: `$keys` and `$values`. 2. The `array_combine()` function combines these two arrays into a single associative array where the elements of `$keys` become the keys, and the elements of `$values` become the corresponding values. 3. If the arrays have an unequal number of elements, the function will return `false`.