合并 2 个具有相同项目数的数组 - php
Merge 2 arrays having same number of items - php
两个数组 $newResponse
和 $key
中的项目数将始终是 same.For $newResponse
的每个项目,我想添加 [=15] 的项目=] 到 $newResponse
的顺序与它们在 $key
数组中的存在顺序相同,如 预期结果 中所述。我如何实现?
dump($newResponse);
是一个如下所示的数组,它是 foreach
循环的结果。
array:4 [
0 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
1 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
2 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
3 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
]
dump($key);
是一个像下面这样的数组,它是另一个 foreach
循环的结果。
array:4[
0=>[
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
1=> [
"totalPoints" => 10
"percent" => 2
"id" => 3
]
2=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
3=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
预期结果:
[
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 10
"percent" => 2
"id" => 3
]
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
这是一种方法:
假设您的数据是:
$newResponse = [
[
"courseId" => 18,
"courseDisplayName" => "qqq"
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
],
[
"courseId" => 18,
"courseDisplayName" => "qqq",
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
]
];
$key = [
[
"totalPoints" => 2,
"percent" => 1.0,
"id" => 2
],
[
"totalPoints" => 10,
"percent" => 2,
"id" => 3
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 6
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 5
]
];
然后我可能会达到 array_map,最近将它用于这种目的:
$out = [];
array_map(function($a,$b) use (&$out) {
$out[] = $a + $b;
},$newResponse,$key);
print_r($out);
注意上面的 +
与数组合并非常相似,我相信在您的用例中可以互换。您可以在此处了解差异:Array_merge versus +
两个数组 $newResponse
和 $key
中的项目数将始终是 same.For $newResponse
的每个项目,我想添加 [=15] 的项目=] 到 $newResponse
的顺序与它们在 $key
数组中的存在顺序相同,如 预期结果 中所述。我如何实现?
dump($newResponse);
是一个如下所示的数组,它是 foreach
循环的结果。
array:4 [
0 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
1 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
2 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
3 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
]
dump($key);
是一个像下面这样的数组,它是另一个 foreach
循环的结果。
array:4[
0=>[
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
1=> [
"totalPoints" => 10
"percent" => 2
"id" => 3
]
2=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
3=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
预期结果:
[
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 10
"percent" => 2
"id" => 3
]
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
这是一种方法:
假设您的数据是:
$newResponse = [
[
"courseId" => 18,
"courseDisplayName" => "qqq"
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
],
[
"courseId" => 18,
"courseDisplayName" => "qqq",
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
]
];
$key = [
[
"totalPoints" => 2,
"percent" => 1.0,
"id" => 2
],
[
"totalPoints" => 10,
"percent" => 2,
"id" => 3
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 6
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 5
]
];
然后我可能会达到 array_map,最近将它用于这种目的:
$out = [];
array_map(function($a,$b) use (&$out) {
$out[] = $a + $b;
},$newResponse,$key);
print_r($out);
注意上面的 +
与数组合并非常相似,我相信在您的用例中可以互换。您可以在此处了解差异:Array_merge versus +