The array_combine function in PHP is used to create a new array by using one array for keys and another array for values. It takes two arrays as input: one for keys and one for values. The keys array will be used as the keys for the new array, and the values array will be used as the corresponding values. It returns a new array with the combined key-value pairs.
Here's the syntax for array_combine:
- $keys: An array to be used as keys.
- $values: An array to be used as values.
The function returns the combined array, or false if the number of elements in $keys and $values are not equal.
In this example, $products is used as the keys, and $prices is used as the values. After combining, $product_prices will be:
// Example 1 $products = [ 'apple', 'banana', 'cherry' ]; $prices = [ 1.0, 0.5, 2.0 ]; $product_prices = array_combine($products, $prices); //output Array ( [apple] => 1 [banana] => 0.5 [cherry] => 2 )
The array_combine function combines the two arrays, using the first as keys and the second as values, resulting in a new associative array.