PHP - Function array_combine()


Advertisements


Syntax

array array_combine ( array $keys, array $values );

Definition and Usage

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Parameters

Parameter Description
keys Array of keys to be used
values Array of values to be used

Return Values

Returns the combined array, FALSE if the number of elements for each array isn't equal or if the arrays are empty.

Example

Try out following example −

<?php 
   $a = array('green', 'red', 'yellow');
   $b = array('avocado', 'apple', 'banana');
   $c = array_combine($a, $b);
   
   print_r($c);
?> 

This will produce following result −

Array([green] => avocado [red] => apple [yellow] => banana)

php_function_reference.htm