We now have a youtube channel. Subscribe!

PHP array_combine() Function

PHP array_combine() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP array_combine() Function.

The array_combine() function takes two different or same arrays as input and then creates a new array using the values from the keys array as keys and 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.NoParameter & 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 built-in function 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 will throw E_WARNING.

Example1

The following is an example where we are 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 are making use of two different arrays to combine them into just one array, but this time around we are using 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 going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the array_count_values() Function.

Feel free to ask your questions where necessary and we 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain