首页 > 解决方案 > 如果给定键的值匹配,则将一个数组元素添加到另一个数组中

问题描述

我在尝试操作具有一些相似性的两个数组时遇到了一些麻烦。

第一个数组具有以下结构:

  array(3) {
    [0]=>
    array(2) {
      ["id_modulo"]=>
      string(3) "114"
      ["nome_modulo"]=>
      string(16) "1. Acessos"
    }
    [1]=>
    array(2) {
      ["id_modulo"]=>
      string(3) "118"
      ["nome_modulo"]=>
      string(27) "4. Área de Vivência"
    }
    [2]=>
    array(2) {
      ["id_modulo"]=>
      string(3) "128"
      ["nome_modulo"]=>
      string(20) "14. Supressão"
    }
  }

第二个的结构:

array(6) {
    [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"
    }
    [2]=>
    array(4) {
      ["id_modulo"]=>
      string(3) "118"
      ["id_pergunta"]=>
      string(3) "549"
      ["pergunta"]=>
      string(76) "Example"
      ["resposta"]=>
      string(1) "C"
    }
    [3]=>
    array(4) {
      ["id_modulo"]=>
      string(3) "114"
      ["id_pergunta"]=>
      string(3) "550"
      ["pergunta"]=>
      string(43) "Example"
      ["resposta"]=>
      string(1) "C"
    }
  }

我想要做的是将第二个数组附加到第一个数组中,其中id_modulo相同。基本上第一个数组有一些模块的详细信息,第二个数组有每个模块的问题和答案。

所以第二个数组中与id_module节点对应的所有问题都必须插入第一个数组。

我已经尝试了很多使用array_merge循环的示例array_push,也遵循了本教程,但无法达到我期望的最终结果。

编辑

输出示例:

[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"
      }
  }

标签: phparraysmultidimensional-array

解决方案


首先创建一个临时数组来存储id_modulo第一个数组中给定的索引。因此,在将item第二个数组推送到第一个数组时,我们不必每次都进行搜索。

$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
)

现在循环遍历第二个数组并将其推送到items基于临时数组的索引处的第一个数组。

// 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
        )

)

推荐阅读