0

I have an array in PHP like the first one below and I want to merge all the subarrays having the same index key so that after merging they appear under one index key.Any help will be appreciated.THANKYOU

Array:

            Array ( 
     [person1] => Array ( 
            [id] => 234
            [acc] => 1
        ) 
     [person1] => Array ( 
            [id] => 345
            [acc] => 2
        ) 
     [person2] => Array ( 
            [id] => 128
            [acc] => 1
        ) 
     [person3] => Array ( 
            [id] => 576
            [acc] => 5
        ) 
     [person3] => Array ( 
            [id] => 628
            [acc] => 1 
        ) 
 )

The Output should be like this:

    Array
(
[person1] => Array
    (
        0 => Array
            (
                [id] => 234
                [acc] => 1
            ),
        1 => Array
            (
                [id] => 345
                [acc] => 2
            )   
    )    
[person2] => Array
    (
        0 => Array
            (
                [id] => 128
                [acc] => 1
            )    
    )
[person3] => Array
    (
        0 => Array
            (
                [id] => 576
                [acc] => 5
            ),
        1 => Array
            (
                [id] => 628
                [acc] => 1
            )   
    )
)
zish
  • 607
  • 2
  • 6
  • 15
  • So loop over the array(s) and add them to another array. Before adding check if the key is already set, if so append, if not add it. – Jonathan Kuhn Jun 22 '17 at 20:32
  • @JonathanKuhn Actually I am new would you please show it by code – zish Jun 22 '17 at 20:37
  • Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](https://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. – Jonathan Kuhn Jun 22 '17 at 20:39
  • Are you sure you have duplicate array key? array dont have duplicate key – Omi Jun 22 '17 at 20:50

0 Answers0