Search code examples
phparraysmultidimensional-arrayparent-childmerging-data

Add rows from one array as child items to an array of parent rows


I'm facing some trouble while trying to manipulate two arrays with some similarities.

The first array has this structure:

$modules = [
    [
        "id_modulo" => "114",
        "nome_modulo" => "1. Acessos"
    ],
    [
    
        "id_modulo" => "118",
        "nome_modulo" => "4. Área de Vivência"
    ],
    [
        "id_modulo" => "128",
        "nome_modulo" => "14. Supressão"
    ]
];

And the second one's structure:

$items = [
    [
        "id_modulo" => "114",
        "id_pergunta" => "547",
        "pergunta" => "Example",
        "resposta" => "C"
    ],
    [
        "id_modulo" => "114",
        "id_pergunta" => "548",
        "pergunta" => "Example",
        "resposta" => "C"
    ],
    [
        "id_modulo" => "118",
        "id_pergunta" => "549",
        "pergunta" => "Example",
        "resposta" => "C"
    ],
    [
        "id_modulo" => "114",
        "id_pergunta" => "550",
        "pergunta" => "Example",
        "resposta" => "C"
    ],
];

What I am trying to do is append the second array into the first one, where id_modulo is the same. Basically the first array has some modules details, and the second one has questions and answers of each module.

So all the questions from the second array that corresponds with the id_module node must be inserted into the first array.

I already tried lots of examples using array_merge, looping with array_push, also followed this tutorial but could not achieve anything like the final result that I expect.

Example of output:

[0]=>
  array(2) {
    ["id_modulo"]=>
    string(3) "114"
    ["nome_modulo"]=>
    string(16) "1. Acessos"
    ["items"]=>
      [0]=>
        array(4) {
          ["id_modulo"]=>
          string(3) "114"
          ["id_pergunta"]=>
          string(3) "547"
          ["pergunta"]=>
          string(58) "Example"
          ["resposta"]=>
          string(1) "C"
        }
      [1]=>
      array(4) {
        ["id_modulo"]=>
        string(3) "114"
        ["id_pergunta"]=>
        string(3) "548"
        ["pergunta"]=>
        string(57) "Example"
        ["resposta"]=>
        string(1) "C"
      }
  }

Solution

  • First create a temp array to store index of a given id_modulo from first array. So while pushing an item from second array to first array, we don't have to search every time.

    $arrModuleIndex     =   [];
    // $arr here is your first array.
    foreach($arr as $key => $data){
        $arrModuleIndex[$data['id_modulo']] = $key;
    }
    
    Output:
    Array
    (
        [114] => 0
        [118] => 1
        [128] => 2
    )
    

    Now loop through second array and push it to items of first array at index based on temporary array.

    // Here $arr2 is your second array
    foreach($arr2 as $data){
        $arr[$arrModuleIndex[$data['id_modulo']]]['items'][] = $data;
    }
    
    Output:
    Array
    (
        [0] => Array
            (
                [id_modulo] => 114
                [nome_modulo] => 1. Acessos
                [items] => Array
                    (
                        [0] => Array
                            (
                                [id_modulo] => 114
                                [id_pergunta] => 547
                                [pergunta] => Example
                                [resposta] => C
                            )
    
                        [1] => Array
                            (
                                [id_modulo] => 114
                                [id_pergunta] => 548
                                [pergunta] => Example
                                [resposta] => C
                            )
    
                        [2] => Array
                            (
                                [id_modulo] => 114
                                [id_pergunta] => 550
                                [pergunta] => Example
                                [resposta] => C
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [id_modulo] => 118
                [nome_modulo] => 4. Área de Vivência
                [items] => Array
                    (
                        [0] => Array
                            (
                                [id_modulo] => 118
                                [id_pergunta] => 549
                                [pergunta] => Example
                                [resposta] => C
                            )
    
                    )
    
            )
    
        [2] => Array
            (
                [id_modulo] => 128
                [nome_modulo] => 14. Supressão
            )
    
    )