PHP array_combine Function | Combine Two Arrays

PHP array_combine is an inbuilt Function in PHP. It generates a new array by combining keys and values of two arrays. The elements of the first array will be the keys to the new array. Similarly, the elements of the second array will be the values of the new array. In this article, we will discuss PHP array_combine Function. Also, we will go through a few examples to illustrate it’s usage.

Syntax

array_combine ($keysArray, $valuesArray)

Parameters

The PHP array_combine Function takes in two mandatory parameters. However, the two parameters should be only single dimension arrays. Also, the number of parameters in both the arrays must be the same. The description of the parameters is given as follows:

  • $keysArray: The first parameter of function is the keysArray. The function will use the values of this array as the keys of the new array.
  • $valuesArray: This is the second parameter to function. The function will use the values of this array as the values of new array.

Return Type

The array_combine Function in PHP returns a new combined array. The elements in $keysArray represent the keys in the new array. Similarly, the values in $valuesArray represent the values in the new array. However, the function returns false if the number of values in the two arrays are not equal.

PHP array_combine Function Diagram Concatly

Examples

Let’s discuss a few examples to demonstrate the working of PHP array_combine Function.

Example 1: Basic Usage

For instance, let’s consider a basic example.

<?php
	$keysArray = array('Dog', 'Cat', 'Mouse');
	$valuesArray = array(4, 5, 9);
	$combinedArray = array_combine($keysArray, $valuesArray);
	print_r($combinedArray);
	/*
	Array
	(
	    [Dog] => 4
	    [Cat] => 5
	    [Mouse] => 9
	)
	*/
?>

In the above example, you can observe that the values of keysArray become the keys of the final array. Also, the values of the valuesArray become the values of the final array. Also, notice that the number of elements in both the arrays is the same.

Example 2: Unequal Number of Elements in Arrays

For example, let’s consider a scenario when the number of elements in both the arrays is not the same. In this case, the PHP array_combine will return False.

<?php
	$keysArray = array('Dog', 'Cat');
	$valuesArray = array(4, 5, 9);
	$combinedArray = array_combine($keysArray, $valuesArray);
	var_dump($combinedArray);
	/*
	bool(false)
	*/
?>

Example 3: Repeating Values in keysArray

For instance, consider an example where there are repeating elements in the keysArray.

<?php
	$keysArray = array('Dog', 'Dog', 'Cat');
	$valuesArray = array(4, 5, 9);
	$combinedArray = array_combine($keysArray, $valuesArray);
	print_r($combinedArray);
	/*
	Notice that the first value in valuesArray is ignored and only the second one prevails
	Array
	(
	    [Dog] => 5
	    [Cat] => 9
	)
	*/
?>

As you can observe in the above example, only the second key prevails when there are repeating values in the keysArray.

Conclusion

In conclusion, we discussed the array_combine Function in PHP. It is very useful in combining two arrays. You can go through other PHP Array Functions on Concatly!
Also, there is a lot to learn about the array_combine Function on the Official Documentation of PHP.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *