我有2个数组。需要将两个数组合并/合并为1个数组。我不知道该怎么做。我的第一个数组看起来像这样:
Array
(
[A626] => Array
(
[name] => MUHAMMAD FIZMIE ZAKARIA
[email] => mfz@mail.edu.my
[empid] => A626
)
[A672] => Array
(
[name] => MOHD FARID ISMADI
[email] =>
[empid] => A672
)
[A1149] => Array
(
[name] => MOHD JAZLI BIN MOHD ZABRI
[email] => mjmz@mail.edu.my
[empid] => A1149
)
)
第二个数组是:
Array
(
[A1149] => Array
(
[empid] => A1149
[online] => 1
[ip] => 127.0.0.1
)
[A626] => Array
(
[empid] => A626
[online] => 0
[ip] =>
)
[A672] => Array
(
[empid] => A672
[online] => 0
[ip] =>
)
)
预期结果应该是这样的:
Array
(
[A626] => Array
(
[name] => MUHAMMAD FIZMIE ZAKARIA
[email] => mfz@mail.edu.my
[empid] => A626
[online] => 0
[ip] =>
)
[A672] => Array
(
[name] => MOHD FARID ISMADI
[email] =>
[empid] => A672
[online] => 0
[ip] =>
)
[A1149] => Array
(
[name] => MOHD JAZLI MOHD ZABRI
[email] => mjmz@mail.edu.my
[empid] => A1149
[online] => 127.0.0.1
[ip] => 1
)
)
请帮帮我。谢谢
答案 0 :(得分:0)
foreach ($arr1 as $key => $user) {
$arr3[$key] = array(
'name' => $user['name'],
'email' => $user['email'],
'empid' => $user['empid'],
'online' => $arr2[$key]['online'],
'ip' => $arr2[$key]['ip'],
);
}
答案 1 :(得分:0)
array_merge_recursive
完全正确that。使用您的阵列,您将在empid
上获得额外的级别,因为它位于两个阵列上。例如,A626
的结果:
[A626] => Array
(
[name] => MUHAMMAD FIZMIE ZAKARIA
[email] => mfz@mail.edu.my
[empid] => Array
(
[0] => A626
[1] => A626
)
[online] => 0
[ip] =>
)
您可以忽略这一点,通过在empid
循环上获取父密钥,您可以更轻松地访问foreach
:
foreach($result as $empid=>$data) {
// ...
}