PHP array_combine() Function
October 29, 2020
Hello folks! welcome to another section of our tutorial on PHP. In this tutorial guide, we are going to be studying about array_combine() Function in PHP.
The PHP array_combine() function takes two different or the same arrays as input and then creates a new array by using the values from the keys array as keys and then the values array is used as the corresponding values.
While passing two arrays in the function, make sure the number of elements in both arrays are the same, else it will return an error.
The PHP array_combine() function takes two different or the same arrays as input and then creates a new array by using the values from the keys array as keys and then the values array is used as the corresponding values.
While passing two arrays in the function, make sure the number of elements in both arrays are the same, else it will return an error.
Syntax
Following below is the syntax to use this function -
array array_combine ( array $keys, array $values );
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | keys (mandatory) First array whose values will be used as the keys to create a new array. |
2 | values (mandatory) Second array whose values will be used as the values to create the new array. |
Return Value
This returns a combined array, else False if the number of elements for each array is not equal, or if the arrays are empty.
PHP Version
This function was first lunched as part of core PHP v 5.0.0.
Errors/Exceptions
If the number of elements in the keys and values arrays does not match, it throws E_WARNING.
Example1
The following is an example where we're using two different arrays to combine them into one array -
<?php $a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [green] => avocado [red] => apple [yellow] => banana )
Example2
The following below is an example where we're using two different arrays to combine them into just a one array, but this time around we are making use of unequal number of elements in both arrays -
<?php $a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple'); $c = array_combine($a, $b); print_r($c); ?>
Output
When the above code is executed, it will produce the following result -
PHP Warning: array_combine(): Both parameters should have an equal number of elements in main.php on line 4
Example3
If two keys are the same, then the second one will dominate but it is absolutely valid -
<?php $a = array('green', 'green', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [green] => apple [yellow] => banana )
Example4
We can create a new array using the same input array, try out the following example -
<?php $a = array('green', 'green', 'yellow'); $c = array_combine($a, $a); print_r($c); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [green] => green [yellow] => yellow )
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about PHP's array_count_values() Function.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.