63

In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.

The problem:

 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);
 
 array_merge($A, $B);

 // result
 [a] => 1 [b] => 2 [c] => 4 [d] => 5

As you see, 'c' => 3 is missed.

So how can I merge all of them with the same keys?

6
  • 6
    array shoud have unique key...that is basic property of an array Commented May 4, 2011 at 9:41
  • 9
    How do you imagine an array with two keys that are the same? Commented May 4, 2011 at 9:44
  • Like they've said, keys must be unique. Consider using 2D arrays instead of associative, then you can merge them without any conflicts. i.e. array(array('a', 1), array('b', 2), array('c', 3), array('c', 4), array('d', 5)) Commented May 4, 2011 at 9:51
  • @Nemoden - yes, that's too strange. But my DB class need to merge WHERE, and PARAMS to binding in mysqli. Anyway, so I need to merge them :) Commented May 4, 2011 at 9:53
  • @kuzey this question does not have a minimal reproducible example and this has led to the posting of answers that deliver different results. By clarifying your scenario via comment (instead of an edit), readers will have a difficult time understanding which kind of answer is appropriate. Your question still lacks an exact desired result. Commented May 15, 2022 at 7:33

6 Answers 6

55

You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.

Sign up to request clarification or add additional context in comments.

5 Comments

Hm... strange... I've changes array_merge to array_merge_recursive and got the same results (array keys renumbered). On PHP.net site, under array_merge, in Example #3 there's a comment: "If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator". And that turned to be true in my case. Where did you read, that array_merge_recursive does not renumbers keys?
@trejder: There are no integer keys in this question, so re-indexing does not apply here. I answered with this in mind.
@jon the link you provide with the answer is broken. Could you inline the code please, if you still have it?
@AdamCameron use the code in the question, just with array_merge_recursive instead of the "plain" version.
@Elyor array_merge and array_merge_recursive are documented to have different behavior for string and integer keys, and for good reason: if they didn't, then the result of array_merge([1], [2]) would be [0 => 2], which is at least "unexpected". So they assume that if you have integer keys, then they don't really matter. If you have integer keys that matter then you must provide explicit logic that takes account of this.
45

Try with array_merge_recursive

$A = [
  'a' => 1,
  'b' => 2,
  'c' => 3,
];

$B = [
  'c' => 4,
  'd'=> 5,
];

$c = array_merge_recursive($A,$B);

echo "<pre>";
print_r($c);
echo "</pre>";

will return

Array
(
    [a] => 1
    [b] => 2
    [c] => Array
        (
            [0] => 3
            [1] => 4
        )

    [d] => 5
)

Comments

7
$arr1 = array(
   "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
   "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
   "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
   "3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
   "4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);

if you want to convert this array as following:

$arr2 = array(
   "0" => array(
          "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
          "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
          "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
          "3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
    ),

    "1" => array(
          "0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
     )
);

so, my answer will be like this:

$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
    $inner_array = array();

    $fid_value = $value['fid'];
    if(!in_array($value['fid'], $unique_array))
    {
            array_push($unique_array, $fid_value);
            unset($value['fid']);
            array_push($inner_array, $value);
            $outer_array[$fid_value] = $inner_array;


    }else{
            unset($value['fid']);
            array_push($outer_array[$fid_value], $value);

    }
}
var_dump(array_values($outer_array));

hope this answer will help somebody sometime.

Comments

3
 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);
 $C = array_merge_recursive($A, $B);
 $aWhere = array();
 foreach ($C as $k=>$v) {

    if (is_array($v)) {
        $aWhere[] = $k . ' in ('.implode(', ',$v).')';
    }
    else {
        $aWhere[] = $k . ' = ' . $v;
    }
 }
 $where = implode(' AND ', $aWhere);
 echo $where;

3 Comments

Has this answer deviated from the asked question? Why am I seeing "where" and "in ()`?
@mickmackusa some of the comments to the original question indicate the question WAS something to do with where statements. I'd assume the question has been redacted. Even despite this my answer is still in sync the the question since it does include the feasible solution of using array_merge_recursive
Ah I see the comment that your answer caters to. This answer is missing its educational explanation. Explaining in a comment, that this answer is not implementing secure querying practices, is not ideal. I didn't read the comment before reading your answer.
3

I just wrote this function, it should do the trick for you, but it does left join

public function mergePerKey($array1,$array2)
    {
        $mergedArray = [];

        foreach ($array1 as $key => $value) 
        {
            if(isset($array2[$key]))
             {
               $mergedArray[$value] = null;

               continue;
             }
            $mergedArray[$value] = $array2[$key];
        }

        return $mergedArray;
    }

3 Comments

Please explain your snippet and include a working demo link.
I second suggestion for explaining the code snippet. But I don't think a working demo link is absolutely necessary here. (in addition to this - how can answers be useful when OP didn't bother to update question to make it more clear (besides polite comments requesting to do so) - it is not really clear what is the intended result in this question).
The reason that I asked for a demo was so that it was clear how this answer does not provide the desired result. At no point did the asker call for null values.
-1

Two entries in an array can't share a key, you'll need to change the key for the duplicate

1 Comment

Expressing that the question is Unclear can be done as a comment under the question or by voting to close as Need Clarity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.