-2

我有 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
    )

)

请帮我。谢谢你

4

2 回答 2

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'],
    );
}
于 2012-06-12T02:58:22.350 回答
0

array_merge_recursive正是这样做的。使用您的阵列,您将获得额外的水平,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) {
    // ...
}
于 2012-06-12T03:15:46.380 回答