The array_combine() function in PHP is a powerful tool that allows you to create an array by using one array for keys and another for values. This function can be particularly useful when you need to associate data in a structured manner.
array_combine(array $keys, array $values): array|false
$keys: An array containing keys.
$values: An array containing values.
If the number of elements in the $keys and $values arrays are equal, array_combine() returns a new array with keys from the $keys array and values from the $values array.
If the arrays are not of equal length, array_combine() returns false.
<?php
$key = ["Key0", "Key1", "Key2"];
$value = ["val0", "val1", "val2"];
$new_array = array_combine($key,$value);
print_r($new_array);
/*
:::::output:::::
Array
(
[Key0] => val0
[Key1] => val1
[Key2] => val2
)
*/
?>
In this example, the array_combine() function takes the $keys array and the $values array and creates a new associative array where the elements in the $keys array become the keys and the elements in the $values array become the corresponding values.
It's important to note that the lengths of the $keys and $values arrays must match for the function to work correctly. If they are not of equal length, the function will return false.