如何在PHP中连接数组值?

时间:2016-05-17 07:03:42

标签: php arrays

我有数组1

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
        )

)

数组2

Array
(
    [0] => Array
        (
            [valuator1] => Wulan  Lastia Permana
        )
)

我想在一个数组中创建数组值。我想要这样的结果:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
            [valuator1] => Wulan  Lastia Permana
        )
)

是否可以像这样加入?

4 个答案:

答案 0 :(得分:11)

您必须使用array_replace_recursive

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
            [valuator1] => Wulan  Lastia Permana
        )

)

结果:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
        )

    [1] => Array
        (
            [valuator1] => Wulan  Lastia Permana
        )

)

您的Eval

如果您使用array_merge_recursive,则output将为:

string QuestionOption_Upsert(string UserCode, string ExamCode, QuesAnswer[] Answer);

答案 1 :(得分:2)

您可以使用array_merge_recursive功能

$result = array_merge_recursive($array1, $array2);

答案 2 :(得分:0)

是的,有可能。让您的第一个数组为$arr1,第二个数组为$arr2

只需为第一个数组指定一个新字段,第一个数组将使用第二个数组key, value pair进行更新。

$arr1[0]['valuator1'] = $arr2[0]['valuator1'];

print_r($arr1);

<强>结果:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
            [valuator1] => Wulan  Lastia Permana
        )

)

答案 3 :(得分:0)

在我看来,这不是最佳选择,但仍会解决您的问题。

foreach ($arr2 as $k => $v) {
    $arr1[$key] = array_merge($arr1[$key], $v);
}

这将遍历第二个数组并将每个值与第一个数组合并。

注意:两个数组中的数组键应该相同,以便工作。