PHP array combine

Last Updated: February 10, 2022

array_combine — Creates a new array by combining two arrays. One array is for the keys and the other array is for the values.

Syntax

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

Supports PHP 5, PHP 7, PHP 8

Parameters

Parameter Description
keys Required. Keys for the array
values Required. Values for  the array

Return Value

This function returns a combined array. false will be returned if the number of elements of both arrays is not equal

Example

<?php

$first = array(1,2,3,4,5);
$second = array('One',"Two","Three","Four","Five");
$third=array_combine($first,$second);
print_r($third);

?>

Output

Array ( 
[1] => One 
[2] => Two 
[3] => Three 
[4] => Four 
[5] => Five 
) 
Test