0

I have two arrays: $arr1 and $arr2. The two arrays have equal keys. I am merging the two arrays with duplicate keys. My output should show the duplicate keys with their corresponding values, for example

the key 22 exists and contains values 333,673,434 

Below is my current code:

<?
    $result = array();
    foreach ($arr1 as $i => $key) 
    {
        $result[] = array($key => $arr2[$i]);
    }

    print_r($result);
?>

Result as below

Array
(
    [0] => Array
        (
            [22] => 333
        )

    [1] => Array
        (
            [22] => 673
        )

    [2] => Array
        (
            [22] => 434
        )

    [3] => Array
        (
            [29] => 67
        )?>

    [4] => Array
        (
            [29] => 98
        )
[5] => Array
        (
            [29] => 656
        )

    [6] => Array
        (
            [28] => 12
        )
}
1
  • 2
    And what is your question? You only described a situation, not what your problem is. (And no...nobody will give you code...show us what you got, and we might be able to help you sort out any mistakes.) Commented May 1, 2013 at 8:45

1 Answer 1

1

Change:

 $result[] = array($key => $arr2[$i]);

To

 $result[$key][] = $arr2[$i];

You should get a array for each index. i.e. for 22, 28 and 29.

On 22, you should get a array containing 333,673 and 434.

If you need that value on comma separated value, then, try

if(is_array($result)&&!empty($result))
    foreach($result as $key => $item)
        $result_new[$key] = implode(',', $result[$key]);
print_r($result_new);
Sign up to request clarification or add additional context in comments.

Comments

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.