• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php array_combine() – Create New Array Given Arrays of Keys and Values

php array_combine() – Create New Array Given Arrays of Keys and Values

April 2, 2022 Leave a Comment

The php array_combine() function creates a new array given two arrays, where one array is used for the keys and the other array is used for the values of the new array.

$array1 = array('bear', 'elephant', 'rabbit');
$array2 = array(1,2,3);
$new_array = array_combine($array1, $array2);

print_r($new_array);

// Output: 
Array
(
    [bear] => 1
    [elephant] => 2
    [rabbit] => 3
)

When working with collections of data in php, the ability to manipulate them and create new collections is very valuable.

One such manipulation is the ability to combine multiple arrays into a new array in php.

The php array_combine() function will take two arrays and “zip” them together where the first array passed to array_combine() will be used for the keys and the second array will be used for the values.

Below is a simple example in php showing how we can use the array_combine() function.

$array1 = array('bear', 'elephant', 'rabbit');
$array2 = array(1,2,3);
$new_array = array_combine($array1, $array2);

print_r($new_array);

// Output: 
Array
(
    [bear] => 1
    [elephant] => 2
    [rabbit] => 3
)

Duplicate Keys and array_combine()

If you have duplicate values in the array which will be the keys of the new array, array_combine() will keep the second key.

Below is an example of what happens when you have duplicate keys and try to use array_combine()

$array1 = array('bear', 'bear', 'rabbit');
$array2 = array(1,2,3);
$new_array = array_combine($array1, $array2);

print_r($new_array);

// Output: 
Array
(
    [bear] => 2
    [rabbit] => 3
)

To keep all of the values, you can define your own function which will take any duplicate keys and then create an array which will store all of the values for those duplicate keys.

Below is a function which will help you handle the case where you have duplicate keys and want to keep all of the values.

function array_combine_dup($keys, $values) {
    $result = array();
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk($result, function(&$v){ $v = (count($v) == 1) ? array_pop($v): $v; });
    return $result;
}

$array1 = array('bear', 'bear', 'rabbit');
$array2 = array(1,2,3);
$new_array = array_combine_dup($array1, $array2)

print_r($new_array);

// Output: 
Array
(
    [bear] => Array
        (
            [0] => 1
            [1] => 2
        )

    [rabbit] => 3
)

Hopefully this article has been useful for you to learn how to use the php array_combine() function.

Other Articles You'll Also Like:

  • 1.  Get Query String from URL in php
  • 2.  Reversing an Array in php with array_reverse() Function
  • 3.  Get First Element of Array in php
  • 4.  php print_r() Function- Get Information about Variables in php
  • 5.  php in_array – Check If Value is in php Array
  • 6.  preg_replace php – Use Regex to Search and Replace
  • 7.  php Delete Files with php Unlink Function
  • 8.  php getimagesize – Get Height, Width and Image File Type
  • 9.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 10.  php Square Root with sqrt Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy