PHP | array_combine() Function
The array_combine() is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of this new array.
Examples:
Input : $array1 = ("Ram", "Akash", "Rishav");
$array2 = ('24', '30', '45');
Output :
Array
(
[Ram] => 24
[Akash] => 30
[Rishav] => 45
)
Input : $array1 = ("65824", "92547", "12045");
$array2 = ('1', '2', '3');
Output :
Array
(
[65824] => 1
[92547] => 2
[12045] => 3
)
Syntax:
array_combine( $keys_array, $values_array )
Parameters: This function accepts two parameters and both are mandatory. The function parameters as listed below:
- $keys_array: This is an array of keys. If illegal values are passed as the key, then it will be converted into a string.
- $values_array: This is an array of values that is to be used in the new array.
Return Value: This function returns a new combined array, in which the elements from first array $keys_array represents keys in new array and the elements from second array $values_array represents the corresponding values in the new array. This function returns false if the number of elements in the two arrays are not same.
Below program illustrates the array_combine() function in PHP:
<?php // PHP program to illustrate the working // of array_combine() function function Combine($array1, $array2) { return(array_combine($array1, $array2)); } // Driver Code $array1 = array("Ram", "Akash", "Rishav"); $array2 = array('24', '30', '45'); print_r(Combine($array1, $array2)); ?> |
Output:
Array
(
[Ram] => 24
[Akash] => 30
[Rishav] => 45
)
Note: The total number of elements in both of the arrays must be equal for the function to execute successfully otherwise it will throw an error.
Reference: https://www.php.net/manual/en/function.array-combine.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.map.set() Function
- D3.js | d3.hcl() Function
- D3.js | d3.lab() Function
- D3.js | d3.set.add() Function
- PHP | each() Function
- PHP | ord() Function
- p5.js | nfs() Function
- p5.js | box() Function
- CSS | hsl() Function
- PHP | cos( ) Function
- PHP | Ds\Map xor() Function
- PHP | tan( ) Function
- PHP | Ds\Map put() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



