PHP array_​combine Function



array_​combine() is an another PHP pre built / default array function, This function is basically allows user creates an array by using the elements from one "keys" array and one "values" array. In the array_combine we must have both array in equal number of elements.

array_​combine() is an another PHP pre built / default array function, This function is basically allows user creates an array by using the elements from one “keys” array and one “values” array. In the array_combine we must have both array in equal number of elements.

Syntax of array_​combine() :

array_combine(array $keys, array $values)

Parameter :
keys (required) : Array of keys
values (required) : Array of values

Return Type / Values :
It returns the combined array with key value combination.

Example 1 : 
<?php
	$first_name = array("Kanav", "Parth", "Prakhar");
	$age = array("4", "10", "11");
	$combine = array_combine($first_name, $age);
	print_r($combine);
?>

Output will be :

Array 
( 
	[Kanav] => 4 
	[Parth] => 10 
	[Prakhar] => 11 
)

Example 2 : If both array is not in equal number of elements it will show an error..

<?php
	$first_name = array("Kanav", "Parth", "Prakhar", "Parikshit");
	$age = array("4", "10", "11");
	$combine = array_combine($first_name, $age);
	print_r($combine);
?>

Output will be : it will show the blank or error.

Reference: https://www.php.net/manual/en/function.array-combine.php

Reference Article : https://www.php.net/manual/en/function.array-combine.php

In the previous post I described regarding the PHP array_​merge Function Please check this article as well.

Hope this article helps you in PHP development and understandings of Array. Stay tuned for more PHP Tutorials, Hope you enjoyed reading, if you need the professional PHP, PHP Framework and Magento 2 Development we can help you, just Click on the Link and send me your requirements.