up vote 0 down vote favorite
share [g+] share [fb]
$a = array('matches' => 
        array(
            '5' => array('weight' => 6),
            '15' => array('weight' => 6),
        )
    );

    $b = array('matches' => 
        array(
            '25' => array('weight' => 6),
            '35' => array('weight' => 6),
        )
    );

    $merge = array_merge($a, $b);

    print_r($merge);

The result of this script is

Array
(
    [matches] => Array
        (
            [25] => Array
                (
                    [weight] => 6
                )

            [35] => Array
                (
                    [weight] => 6
                )

        )

)

But why?

I want the result was this:

Array
(
    [matches] => Array
        (
            [5] => Array
                (
                    [weight] => 6
                )

            [15] => Array
                (
                    [weight] => 6
                )
            [25] => Array
                (
                    [weight] => 6
                )

            [35] => Array
                (
                    [weight] => 6
                )

        )

) 
link|improve this question

feedback

5 Answers

up vote 7 down vote accepted

Because the key 'matches' in the first array is being overwritten by the same key in the second. You need to do this instead:

$merge = array('matches' => array());
$a = array(
    'matches' => array(
        '5' => array('weight' => 6),
        '15' => array('weight' => 6)
    )
);

$b = array(
    'matches' => array(
        '25' => array('weight' => 6),
        '35' => array('weight' => 6)
    )
);

$merge['matches'] = array_merge($a['matches'], $b['matches']);

print_r($merge);

UPDATE

In order to preserve the numeric keys, you'll have to do this:

$merge['matches'] = $a['matches'] + $b['matches'];

If using the array union operator like this, just remember the following from php.net:

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

http://php.net/manual/en/function.array-merge.php

link|improve this answer
how i can save a "key" matches? need "5,15,25,35", but have "0,1,2,3" – Isis Oct 25 '10 at 21:59
1  
You'll have to stop using array_merge. According to PHP docs, array_merge will always renumber numeric keys, even if you try using numbers in strings like array('1' => 'something', '2' => 'something');. In order to preserve keys, you'll have to use a + array union. I'll update my answer. – Stephen Oct 25 '10 at 22:03
1  
1  
@Isis: Cool function! Useful for when you don't know the array's structure. But I would recommend a more surgical approach when you know exactly what structure you have, like in the problem at hand. – Stephen Oct 25 '10 at 22:07
great. thanks – Isis Oct 25 '10 at 22:10
feedback

Try using array_merge_recursive instead of array_merge.

link|improve this answer
feedback

You're merging the top-level of the array. The array_merge() function is seeing that they both contain a "matches" element, so it is picking one as the merged result.

If you want it to scan deeper, you need to run array_merge() at the next level down in the array. In this case it's relatively simple because you only have one element at the top level of each array, so you'd only need a single line - Something like this:

$merge['matches'] = array_merge($a['matches'], $b['matches']);

A more complex array structure would need more work, though.

Hope that helps.

link|improve this answer
feedback

Try:

$merge = array();
$merge['matches'] = array_merge($a['matches'], $b['matches']);

Your problem is that you are merging the top level $a array with $b and both of these have a "matches" index so it's accepting $b's version of that element. You can get around this by explicitly merging in that index's array ($a['matches'] with $b['matches']) and assigning it to the merged array ($merge['matches']) which will result in the expected behavior.

Alternatively array_merge_recursive will do what you want.

link|improve this answer
feedback

Well, you could do it manually:

    $mergedArray = array();    

    foreach($a as $key => $value)
    {
        $mergedArray[$key] = $value;
    }

    foreach($b as $key => $value)
    {
        $mergedArray[$key] = $value;
    }
link|improve this answer
1  
This will result in the exact same problem the original poster already has. In fact, he would get identical output to what he suggested he was trying to fix. – M2tM Oct 25 '10 at 21:36
feedback

Your Answer

 
or
required, but never shown

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