PHP - 基于Key映射两个数组

时间:2015-08-27 11:47:33

标签: php arrays explode array-merge

用户数组

print_R($user_array);
Array
(
    [0] => Array
        (
            [SKILL_NAME] => Application Software
            [EXPERIENCE_BAND] => 15+
            [SITE_STATUS] => Onsite
            [NO_OF_RESOURCE] => 1
            [ACTUAL_HOURS] => 16
        )

    [1] => Array
        (
            [SKILL_NAME] => Application Software
            [EXPERIENCE_BAND] => 2-4
            [SITE_STATUS] => Onsite
            [NO_OF_RESOURCE] => 1
            [ACTUAL_HOURS] => 96
        )

)

值数组

print_R($value_array);
Array
(
    [RATE_PER_HOUR] => 80,50
    [MARKUP_PERCENT] => 5,10
    [TOTAL_COST] => 8064.00,880.00
)

Exploding Value array RATE_PER_HOUR

$rate_per_hour = explode(',', $array_data['RATE_PER_HOUR']);

以下输出。 MARKUP_PERCENTTOTAL_COST

也是如此
Array
(
    [0] => 80
    [1] => 50
)

我如何在两个数组上面映射,以便输出如下所示。数组映射基于密钥完成,以维护正确的数据。

Array
(
    [0] => Array
        (
            [SKILL_NAME] => Application Software
            [EXPERIENCE_BAND] => 15+
            [SITE_STATUS] => Onsite
            [NO_OF_RESOURCE] => 1
            [ACTUAL_HOURS] => 16
            [RATE_PER_HOUR] => 80
            [MARKUP_PERCENT] => 5
            [TOTAL_COST] => 8064.00
        )

    [1] => Array
        (
            [SKILL_NAME] => Application Software
            [EXPERIENCE_BAND] => 2-4
            [SITE_STATUS] => Onsite
            [NO_OF_RESOURCE] => 1
            [ACTUAL_HOURS] => 96
            [RATE_PER_HOUR] => 50
            [MARKUP_PERCENT] => 10
            [TOTAL_COST] => 880.00
        )

)

1 个答案:

答案 0 :(得分:0)

分解Value数组中的每个值。循环遍历并将它们添加到User数组的相应元素。

foreach ($value_array as $key => $data) {
    $data_array = explode(',', $data);
    foreach ($data_array as $index => $val) {
        $user_array[$index][$key] = $val;
    }
}

DEMO