Home >>PHP Array Functions >PHP array_combine() Function

PHP array_combine() Function

PHP array_combine() Function is used to combine two arrays and create a new array by using one of the given array for keys and another array for values.It accepts two parameters and both are mandatory. It returns a new array, in which the elements of first arrayrepresents keys in the new array and the elements of second array represents the corresponding values in the new array.

Syntax:

array_combine($keys, $values);

Parameter Values

Parameter Description
keys This is a required parameter. This parameter defines an array of keys.
values This is a required parameter. This parameter defines an array of values.

Here is an example of array_combine() function in PHP:

<html>
<body>
<pre>
<?php
$z=array_combine(array("1","2","3"),array("A","B","C"));
print_r($z);
?>
</pre>
</body>
</html>
Output:
Array
(
    [1] => A
    [2] => B
    [3] => C
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array("1","2","3");
$y=array("A","B","C");
$z=array_combine($x,$y);
print_r($z);
?>
</pre>
</body>
</html>
Output:
Array
(
    [1] => A
    [2] => B
    [3] => C
)

No Sidebar ads