1

I am stuck with simple thing (at least I think it's simple),

I have an output from the database with duplicated entries and I would like to merge them. The output looks like this:

 ID   Name   Week   Active
 1    Steve  10     true
 2    Steve  11     false 
 3    Steve  12     true
 4    John   10     true
 5    John   11     false
 6    Emma   10     true
 7    Jane   11     true

So the array should look like this:

[
    [0] => [
        'id'     => 1,
        'name'   => 'Steve',
        'weeks' => [
             [0]  => [
                 'active' => true,
                 'week'   => 10
             ],
             [1]  => [
                 'active' => false,
                 'week'   => 11
             ],
             [2]  => [
                 'active' => true,
                 'week'   => 12
             ]
         ]  
    ],
    [1] => [
        'id'     => 2,
        'name'   => 'John',
        'weeks' => [
             [0]  => [
                 'active' => true,
                 'week'   => 10
             ],
             [1]  => [
                 'active' => false,
                 'week'   => 11
             ]
         ]  
    ],
    [2] => [
        'id'     => 3,
        'name'   => 'Emma',
        'weeks' => [
             [0]  => [
                 'active' => true,
                 'week'   => 10
             ]
         ]  
    ],
    [3] => [
        'id'     => 4,
        'name'   => 'Jane',
        'weeks' => [
             [0]  => [
                 'active' => false,
                 'week'   => 11
             ]
         ]  
    ]
]

Is it easy to accomplish?

Burkhard
  • 14,596
  • 22
  • 87
  • 108
undefinedman
  • 620
  • 1
  • 11
  • 25

1 Answers1

0

You can use JSON as a workaround, where duplicated entries in the dictionary can be converted to an array, see https://stackoverflow.com/a/61416136/7471760

ferdymercury
  • 698
  • 4
  • 15