• 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
  • Ruby
  • 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.  How to Reverse Array in php Without a Function or Other Variables
  • 2.  php array_merge() – Merge One or More Arrays Together by Appending
  • 3.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 4.  Delete Cookie Using php
  • 5.  In PHP isset Method is Used For Checking if Variable is Defined
  • 6.  php array_shift() – Remove the First Element from Array
  • 7.  php array_walk() – Modify Elements in Array with Callback Function
  • 8.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 9.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 10.  Get Last Day of Month in php

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy