php - 如果给定键的值匹配,则将一个数组元素添加到另一个数组中
问题描述
我在尝试操作具有一些相似性的两个数组时遇到了一些麻烦。
第一个数组具有以下结构:
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"
}
}
解决方案
首先创建一个临时数组来存储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
)
)
推荐阅读
- f# - F# 检查列表是否为空
- c++ - 在轮盘游戏c ++中的do / while循环中预期
- python - 在python中进行归一化的多等高线图
- react-admin - React-admin dataProvider 不会触发 authProvider 的 AUTH_ERROR
- wordpress - 使用 wp_delete_post 删除帖子时,其返回意外
- angular - 我想找出在 Ag-Grid 中编辑了哪一行?
- jquery - 如何将显示位置设置为 bootstrap-timepicker 小部件?
- c - 如何使矮小的部分在 elf 文件中加载到内存中?
- javascript - 如何使用原生 JavaScript 实现无限滚动?
- ios - 单击ios中的按钮时如何打开自定义弹出窗口