Array merge key value php

(PHP 5, PHP 7, PHP 8)

array_combineCreates an array by using one array for keys and another for its values

Description

array_combine(array $keys, array $values): array

Parameters

keys

Array of keys to be used. Illegal values for key will be converted to string.

values

Array of values to be used

Return Values

Returns the combined array.

Errors/Exceptions

As of PHP 8.0.0, a ValueError is thrown if the number of elements in keys and values does not match. Prior to PHP 8.0.0, a E_WARNING was emitted instead.

Changelog

VersionDescription
8.0.0 array_combine() will now throw a ValueError if the number of elements for each array is not equal; previously this function returned false instead.

Examples

Example #1 A simple array_combine() example

$a = array('green''red''yellow');
$b = array('avocado''apple''banana');
$c array_combine($a$b);print_r($c);
?>

The above example will output:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

See Also

  • array_merge() - Merge one or more arrays
  • array_walk() - Apply a user supplied function to every member of an array
  • array_values() - Return all the values of an array

loureirorg at gmail dot com

9 years ago

If two keys are the same, the second one prevails.

Example:
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => 2
    [b] => 3
)

But if you need to keep all values, you can use the function below:

function array_combine_($keys, $values)
{
   
$result = array();
    foreach (
$keys as $i => $k) {
       
$result[$k][] = $values[$i];
    }
   
array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
    return   
$result;
}
print_r(array_combine_(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => Array
        (
            [0] => 1
            [1] => 2
        )

    [b] => 3
)

welcome at el hyphen mustafa

6 years ago

Further to loreiorg's script
in order to preserve duplicate keys when combining arrays.

I have modified the script to use a closure instead of create_function

Reason: see security issue flagged up in the documentation concerning create_function

function array_combine_($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;
}
?>

claude dot pache at gmail dot com

12 years ago

array_combine() has a strange bug/misfeature (as of PHP 5.3.2): There is no logical reason for throwing a warning and returning FALSE, instead of returning (see http://bugs.php.net/bug.php?id=34857). Here is a quick workaround:
function array_real_combine($a, $b)
{
    return
$a===array() && $b===array() ? array() : array_combine($a, $b);
}
?>

quecoder at gmail

14 years ago

// If they are not of same size, here is solution: $abbreviations = array("AL", "AK", "AZ", "AR", "TX", "CA");
$states = array("Alabama", "Alaska", "Arizona", "Arkansas");
function
combine_arr($a, $b)
{
   
$acount = count($a);
   
$bcount = count($b);
   
$size = ($acount > $bcount) ? $bcount : $acount;
   
$a = array_slice($a, 0, $size);
   
$b = array_slice($b, 0, $size);
    return
array_combine($a, $b);
}
$combined = combine_arr($abbreviations, $states);
print_r($combined); //  Output
//  Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona
//  [AR] => Arkansas )
?>

zequez at gmail dot com

11 years ago

If two keys are the same, the second one prevails.
Example:
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => 2
    [b] => 3
)

g.REMOVETHIS.vincendon AT vithemis.com

10 years ago

I was looking for a function that could combine an array to multiple one, for my MySQL GROUP_CONCAT() query, so I made this function.

    function array_combine_array(array $keys)
    {
       
$arrays = func_get_args();
       
$keys = array_shift($arrays);/* Checking if arrays are on the same model (array('INDEX'=> array()) or array()) */
       
$check = count(array_unique(array_map('is_array',array_map('current',$arrays)))) === 1;
        if (!
$check) { trigger_error('Function array_combine_array() expects all parameters to be same type array or array of array',E_USER_NOTICE); return array(); }/* Checking the model of arrays, array('INDEX' => array()) or Array() */
       
$assocArray = is_array(array_shift(array_map('current',$arrays)));/* If empty $Keys is given, we fill an empty array */
       
if (empty($keys)) $keys = array_keys(array_fill(0,max(($assocArray) ? array_map('count',array_map('current',$arrays)) : array_map('count',$arrays)),'foo'));/* Init */
       
$ret=array();$i=0;
       
/* Cycling on each keys values, making an offset for each */
       
foreach($keys as $v)
        {
           
/* Cycling on arrays */
           
foreach ($arrays as $k)
            {
                if (
$assocArray)
                {
                   
/* Getting the index of the element */
                   
$key = key($k);
                   
/* If the offset exists, we place it */
                   
$ret[$v][$key] = isset($k[$key][$i]) ? $k[$key][$i]:false;
                }
               
/* Making the array with auto-made index */
               
else
                   
$ret[$v][] = isset($k[$i]) ? $k[$i]: false;
            }
           
/* Getting the next offset */
           
$i++;
        }
        return
$ret;
    }
/* Examples */
   
$r = array(1,2,4,10);$a1 = array('one','two','four','ten');
   
$a2 = array('un','deux','quatre','dix');
   
$a3 = array('uno','dos','quatro','diez');print_r(array_combine_array($r,array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); /* Associative index, associative subarray indexes */
    
print_r(array_combine_array($r,$a1,array('french' => $a2),array('spanish' => $a3))); /* Ouputs Error */
    
print_r(array_combine_array($r,$a1,$a2,$a3)); /* Associative index, auto-made subarray indexes */
    
print_r(array_combine_array(array(),array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); /* Auto-made index, associative subarray indexes */
?>

dejiakala at gmail dot com

10 years ago

I needed to read CSV files into associative arrays with column headers as keys. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr() below allowed me to pad either array or not when parsing my CSVs to arrays.
$a is the array of header columns and $b is an array of the current row retrieved with fgetcsv()

function array_combine_special($a, $b, $pad = TRUE) {
   
$acount = count($a);
   
$bcount = count($b);
   
// more elements in $a than $b but we don't want to pad either
   
if (!$pad) {
       
$size = ($acount > $bcount) ? $bcount : $acount;
       
$a = array_slice($a, 0, $size);
       
$b = array_slice($b, 0, $size);
    } else {
       
// more headers than row fields
       
if ($acount > $bcount) {
           
$more = $acount - $bcount;
           
// how many fields are we missing at the end of the second array?
            // Add empty strings to ensure arrays $a and $b have same number of elements
           
$more = $acount - $bcount;
            for(
$i = 0; $i < $more; $i++) {
               
$b[] = "";
            }
       
// more fields than headers
       
} else if ($acount < $bcount) {
           
$more = $bcount - $acount;
           
// fewer elements in the first array, add extra keys       
           
for($i = 0; $i < $more; $i++) {
               
$key = 'extra_field_0' . $i;
               
$a[] = $key;
            }

                    }
    }

        return

array_combine($a, $b);
}
?>

ilanfir at gmail dot com

7 years ago

I recently had to flip an array and group the elements by value, this snippet will do that:
function flipAndGroup($input) {
   
$outArr = array();
   
array_walk($input, function($value, $key) use (&$outArr) {
        if(!isset(
$outArr[$value]) || !is_array($outArr[$value])) {
           
$outArr[$value] = [];
        }
       
$outArr[$value][] = $key;
    });
    return
$outArr;
}
?>

Example:
$users_countries = array(
   
'username1' => 'US',
   
'user2' => 'US',
   
'newuser' => 'GB'
);
print_r(flipAndGroup($users_countries));
?>

Returns:
Array
(
    [US] => Array
        (
            [0] => username1
            [1] => user2
        )

    [GB] => Array
        (
            [0] => newuser
        )
)

bradentkeith at dot dontspam dot gmail dot com

12 years ago

I needed a function that would take keys from one unequal array and combine them with the values of another. Real life application:
Select 4 product types.
Each product has a serial.
There are 4 sets of products.

            function array_combine2($arr1, $arr2) {
           
$count1 = count($arr1);
           
$count2 = count($arr2);
           
$numofloops = $count2/$count1;$i = 0;
            while(
$i < $numofloops){
               
$arr3 = array_slice($arr2, $count1*$i, $count1);
               
$arr4[] = array_combine($arr1,$arr3);
               
$i++;
            }

                        return

$arr4;
        }
   
?>

Input:
Array
(
    [0] => SMART Board
    [1] => Projector
    [2] => Speakers
    [3] => Splitter
)
, Array
(
    [0] => serial to smart board1
    [1] => serial to projector 1
    [2] => serial to speakers 1
    [3] => serials to splitter 1
    [4] => serials to smart board 2
    [5] => serials to projector 2
    [6] => serials to speakers 2
    [7] => serials to splitter 2
)

Array
(
    [0] => Array
        (
            [SMART Board] => serial to smart board1
            [Projector] => serial to projector 1
            [Speakers] => serial to speakers 1
            [Splitter] => serials to splitter 1
        )

    [1] => Array
        (
            [SMART Board] => serials to smart board 2
            [Projector] => serials to projector 2
            [Speakers] => serials to speakers 2
            [Splitter] => serials to splitter 2
        )

)

PHPNewbie

5 years ago

This will seem obvious to some, but if you need to preserve a duplicate key, being you have unique vars, you can switch the array_combine around, to where the vars are the keys, and this will output correctly.

This [default] formula auto-removes the duplicate keys.

$i=0;
foreach (array_combine($keys, $vars) as $key => $var)
{
$i=$i;
echo($key);
echo " ";
echo($var);
}

This formula accomplishes the same thing, in the same order, but the duplicate "keys" (which are now  vars) are kept.

$i=0;
foreach (array_combine($vars, $keys) as $var => $key)
{
$i=$i;
echo($key);
echo " ";
echo($var);
}

I know, I'm a newbie, but perhaps someone else will need this eventually. I couldn't find another solution anywhere.

Dan LaManna

11 years ago

I needed a function that truncated extra values, and only went as far as keys without throwing a warning as array_combine does.

function safeArrayCombine($keys, $values) {
   
$combinedArray = array();

            for (

$i=0, $keyCount = count($keys); $i < $keyCount; $i++) {
        
$combinedArray[$keys[$i]] = $values[$i];
    }

            return

$combinedArray;
}
?>

Khaly

14 years ago

This is the function for PHP4 :

function array_combine($arr1,$arr2) {
  
$out = array();
   foreach(
$arr1 as $key1 => $value1)    {
   
$out[$value1] = $arr2[$key1];
   }
   return
$out
} ?>

Mike Jean

14 years ago

Khaly's PHP4 code below does not work correctly in all cases. Consider when your array consists of floats:

$okay

= array(0, 10, 20, 30);
$not_okay = array(0, 0.5, 1, 1.5);$foo = array_combine($okay, $okay);
$bar = array_combine($not_okay, $not_okay);/*

Results:

$foo = {
  [0]=> int(0)
  [10]=> int(10)
  [20]=> int(20)
  [30]=> int(30)
}

$bar = {
  [0]=> float(0.5)
  [1]=> float(1.5)
}

*/

?>

What can you do? In my case, I was just zipping up some select-box options, so I converted everything in my floats to strings.

Anonymous

8 months ago

Here a way to manage no uniqueness keys case. Values of duplicate keys are put in an array.

function array_combine_multi(array $keys, array $values)
{
    if (
count($keys) !== count($values)) {
        throw new
Exception('Arrays count mismatch');
    }
   
$result = [];
    foreach (
$values as $i => $value) {
        if (!isset(
$result[$keys[$i]])) {
           
$result[$keys[$i]] = $value;
        } elseif (!
is_array($result[$keys[$i]])) {
           
$result[$keys[$i]] = [$result[$keys[$i]], $value];
        } else {
           
$result[$keys[$i]][] = $value;
        }
    }
    return
$result;
}
$keys = ['key-1', 'key-2', 'key-3', 'key-2', 'key-2'];
$values = ['value-1', 'value-2', 'value-3', 'value-4', 'value-5'];
$result = array_combine_multi($keys, $values);
print_r($result);/*
Gives :

Array (
    [key-1] => value-1
    [key-2] => Array (
        [0] => value-2
        [1] => value-4
        [2] => value-5
    )
    [key-3] => value-3
)
*/

douglasrich9215 at gmail dot com

4 years ago

I had an epiphany when try to handle NON-ASSOCIATIVE array forms in my controller. This little one liner can pretty much edit ANY kind of non-associative array. For example this one just returns an array of values inputed by a new user.

The $data value is the the json_decoded() value of a register form.
Here is used str_replace, you could definitely do a number of things like preg matches and other things.

$readyToProcessForUser = array_combine(str_replace("new_user_", "", array_keys($data)), $data);

You could also do the same for the values.

$readyToProcessForUser = array_combine(array_keys($data), str_replace("-", "", $data));

Or BOTH!
Use full if you don't want to walk an entire array and the keys through the same callback.

$readyToProcessForUser = array_combine(array_walk('trim', array_keys($data)), array_walk('custom_callback', array_values($data)));

elite1228 at 163 dot com

6 years ago

$item = array();
array_map(
    function($element) use($column_name,$key,&$item){
        $item[$element[$key]] = $element[$column_name];
    },
    $data
);

neoyahuu at yahoo dot com

15 years ago

Some tips for merging same values in an array

$array1 = array(1,2,3,4,5,6,7,8,9,10,11,12);
$array2 = array(1,2,3,13);$merged = array_merge($array1,$array2);// output normal array_merge
echo '

After array_merge :
';
print_r($merged);
echo
'
';// do double flip for merging values in an array
$merged = array_flip($merged);
$merged = array_flip($merged);// Output after
echo '
After Double Flip : 
';
print_r($merged);
echo
'
'
;
?>

Output ::

After array_merge :
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 1
    [13] => 2
    [14] => 3
    [15] => 13
)

After Double Flip :
Array
(
    [12] => 1
    [13] => 2
    [14] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [15] => 13
)

ismorodin at hotmail dot com

8 years ago

Function eliminates the error Throws E_WARNING if the number of elements in keys and values ​​does not match.

function arrCombine($arr1 = array(),$arr2 = array()){
            if(is_array($arr1) && is_array($arr2)):
                $cntArr1 = count($arr1);
                $cntArr2 = count($arr2);
                $difference = max($cntArr1,$cntArr2) - min($cntArr1,$cntArr2);
                if($cntArr1 > $cntArr2):
                    for ($i=1;$i<=$difference;$i++){
                        $arr2[$cntArr2+$i] = $cntArr2 + 1;
                    }
                    return array_combine($arr1,$arr2);
                elseif($cntArr1 < $cntArr2):
                    for ($i=1;$i<=$difference;$i++){
                        $arr1[$cntArr1+$i] = count($arr1) + 1;
                    }
                    return array_combine($arr1,$arr2);
                else:
                    return array_combine($arr1,$arr2);
                endif;
            endif;
    }
$array = [1,4,5,6,7,8];
$array2 = ['john','smith'];

var_dump( arrCombine($array,$array2 ));

array (size=6)
  1 => string 'john' (length=4)
  4 => string 'smith' (length=5)
  5 => int 3
  6 => int 3
  7 => int 3
  8 => int 3

var_dump( arrCombine($array2,$array) );

array (size=6)
  'john' => int 1
  'smith' => int 4
  3 => int 5
  4 => int 6
  5 => int 7
  6 => int 8

J.D.D.

14 years ago

This may be obvious, but I don't see anything about it on the manual page, so a friendly warning...  The array you are using as keys must have all unique values.  If not, array elements get dropped. 

$arr_notUnique = array('one' , 'one' , 'two');
$arr_b = array('red' , 'green' , 'blue'); $arr_combo = array_combine($arr_notUnique, $arr_b);
?>

Results:  Array ( [one] => green [two] => blue )

NOT:  Array ( [one] => red [one] => green [two] => blue )

xavier at api-meal dot eu

13 years ago

/**
* Return alternatives defined by values of each parameters.
*
* Exemple :
*
* array_alternatives(array('foo','bar'), array('baz', 'qux'));
* array(
*     array('foo', 'baz'),
*     array('bar', 'baz'),
*     array('foo', 'qux'),
*     array('bar', 'qux'),
* );
*
* array_alternatives(array('a'), array('simple-minded'), array('solution'));
* array(
*     array('a', 'simple-minded', 'solution')
* );
*
* array_alternatives(array('a'), array('red', 'blue'), array('car'));
* array(
*     array('a', 'red',  'car'),
*     array('a', 'blue', 'car'),
* );

* @param array $first_element
* @param array $second_element
* @return array
* @author Xavier Barbosa
*/
function array_alternatives(array $first_element, array $second_element)
{
   
$lists = func_get_args();
   
$total_lists = func_num_args();

        for(

$i=0; $i<$total_lists; $i++)
    {
       
$list =& $lists[$i];
        if (
is_array($list) === FALSE)
            throw new
Exception("Parameter $i is not an array.");
        if (
count($list) === 0)
            throw new
Exception("Parameter $i has no element.");
        unset(
$list);
    }
// Initialize our alternatives
   
$alternatives = array();
    foreach(
$lists[0] as &$value)
    {
       
array_push($alternatives, array($value));
        unset(
$value);
    }
    unset(
$lists[0]);// Process alternatives
   
for($i=1; $i<$total_lists; $i++)
    {
       
$list =& $lists[$i];$new_alternatives = array();
        foreach(
$list as &$value)
        {
            foreach(
$alternatives as $_)
            {
               
array_push($_, $value);
               
array_push($new_alternatives, $_);
            }
        }
// Rotate references, it's cheaper than copy array like `$alternatives = $new_alternatives;`
       
$alternatives =& $new_alternatives;
        unset(
$new_alternatives, $list, $lists[$i]);
    }

        return

$alternatives;
}
?>

aaron at aaronholmes dot net

9 years ago

You can create an array hashset from a flat array, storing both keys and values, with array_combine(). This works with duplicate values in the array too.

$flat_array = array(
       
'one',
       
'two',
       
'three',
       
'three',
       
'four',
       
'one'
);$set = array_combine($flat_array, $flat_array);var_dump($set);
?>

This outputs the following:

array(4) {
    ["one"]=>
        string(3) "one"
    ["two"]=>
        string(3) "two"
    ["three"]=>
        string(5) "three"
    ["four"]=>
        string(4) "four"
}

info at maisuma dot jp

7 years ago

array_combine() returns NULL instrad of FALSE, when non-array parameters are given (issuing warning).

  //NULL
 
var_dump(array_combine('string',array(42)));
?>

What is the difference between Array_merge () and Array_merge_recursive () in PHP?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

How can I merge two arrays in PHP without duplicates?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

What is difference between Array_merge and Array_combine in PHP?

This function is used to merge the elements or values of two or more arrays together into a single array. ... PHP..

How can I get common values from two arrays in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.