Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up
$array1 = array("$name1" => "$id1");

$array2 = array("$name2" => "$id2", "$name3" => "$id3");

I need a new array combining all together, i.e. it would be

$array3 = array("$name1" => "$id1", "$name2" => "$id2", "$name3" => "$id3");

What is the best way to do this?

Sorry, I forgot, the ids will never match each other, but technically the names could, yet would not be likely, and they all need to be listed in one array. I looked at array_merge but wasn't sure if that was best way to do this. Also, how would you unit test this?

share|improve this question
5  
Sounds like you're probably just looking for array_merge. – Corbin Nov 1 '12 at 2:47
up vote 57 down vote accepted

array_merge() is more efficient but there are a couple of options:

$array1 = array("id1" => "value1");

$array2 = array("id2" => "value2", "id3" => "value3", "id4" => "value4");

$array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/);
$array4 = $array1 + $array2;

echo '<pre>';
var_dump($array3);
var_dump($array4);
echo '</pre>';
share|improve this answer
5  
What is benefit of array_merge over using the operator? – jsteinmann Nov 1 '12 at 3:01
13  
Array Union (+): The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten. array_merge(): If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. If all of the arrays contain only numeric keys, the resulting array is given incrementing keys starting from zero. – Samuel Cook Nov 1 '12 at 3:04
7  
It should also be noted that array_merge will return NULL if any of the arguments are NULL. – SeanWM Mar 6 '14 at 19:07

Check out array_merge().

$array3 = array_merge($array1, $array2);
share|improve this answer
    
repeated answer and i wonder this answer is voted 10 UP??? many friends on Stack haahh!! – Sudhanshu Saxena Jun 4 '15 at 6:52
7  
@SudhanshuSaxena Considering I was the first person to post it, I don't see how it could be a repeat answer. – Brad Jun 4 '15 at 14:25

I use a wrapper around array_merge to deal with SeanWM's comment about null arrays; I also sometimes want to get rid of duplicates. I'm also generally wanting to merge one array into another, as opposed to creating a new array. This ends up as:

/**
 * Merge two arrays - but if one is blank or not an array, return the other.
 * @param $a array First array, into which the second array will be merged
 * @param $b array Second array, with the data to be merged
 * @param $unique boolean If true, remove duplicate values before returning
 */
function arrayMerge(&$a, $b, $unique = false) {
    if (empty($b)) {
        return;  // No changes to be made to $a
    }
    if (empty($a)) {
        $a = $b;
        return;
    }
    $a = array_merge($a, $b);
    if ($unique) {
        $a = array_unique($a);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.