在PHP第二个数组中为每个匹配的ID添加值到第一个数组

时间:2019-06-17 18:02:03

标签: php arrays sorting multidimensional-array

我有两个这样的数组:

Array1

$array1 = Array
(
    0 => Array
        (
            'ID' => 101,
            'Code' => 1075,
            'Date' => '2012-03-03 17:13:12.433'
        ),

    1 => Array
        (
            'ID' => 103,
            'Code' => 175,
            'Date' => '2012-09-05 20:30:02.217'
        ),

    2 => Array
        (
            'ID' => 109,
            'Code' => 178,
            'Date' => '2012-07-05 20:30:02.217'
        )

);

Array2

$array2 = Array
(
    0 => Array
        (
            'Amount' => 1234,
            'ID' => 101
        ),

    1 => Array
        (
            'Amount' => 5656,
            'ID' => 101
        ),
    2 => Array
        (
            'Amount' => 1342,
            'ID' => 103
        ),

    3 => Array
        (
            'Amount' => 0,
            'ID' => 0
        )

);

我正在使用下面的代码在两个数组上执行连接:

$arr2 = array_column($array2, "ID");

$finalArray = array();
foreach($array1 as $arr){
    $key = array_search($arr['ID'], $arr2);
    if($key ===false){
        $key = array_search(0, $arr2);
    }
    unset($array2[$key]['ID']);
    $finalArray[] =     array_merge($arr,$array2[$key]);
}

print_r($finalArray);

使用以上代码的当前输出为:

finalArray

    Array
(
    [0] => Array
        (
            [ID] => 101
            [Code] => 1075
            [Date] => 2012-03-03 17:13:12.433
            [Amount] => 1234 //considers only the first entry of ID 101
        )

    [1] => Array
        (
            [ID] => 103
            [Code] => 175
            [Date] => 2012-09-05 20:30:02.217
            [Amount] => 1342
        )

    [2] => Array
        (
            [ID] => 109
            [Code] => 178
            [Date] => 2012-07-05 20:30:02.217
            [Amount] => 0
        )

)

但是,由于在 array2 中,有两个ID为101的条目,但是上面的代码仅对匹配ID为第一个匹配项。 预期的输出是:

所需的输出

   Array
(
    [0] => Array
        (
            [ID] => 101
            [Code] => 1075
            [Date] => 2012-03-03 17:13:12.433
            [Amount] => 1234 //amount for first entry of ID 101
        )
    [1] => Array
        (
            [ID] => 101
            [Code] => 1075
            [Date] => 2012-03-03 17:13:12.433
            [Amount] => 5656//amount for second entry of ID 101
        )
    [2] => Array
        (
            [ID] => 103
            [Code] => 175
            [Date] => 2012-09-05 20:30:02.217
            [Amount] => 1342
        )

    [3] => Array
        (
            [ID] => 109
            [Code] => 178
            [Date] => 2012-07-05 20:30:02.217
            [Amount] => 0
        )

)

我不知道要使用什么循环。 该代码应为array2的每个匹配ID添加行。 如何修改当前代码,使其提供所需的输出?

2 个答案:

答案 0 :(得分:1)

使结果数组成为以ID为键的关联数组。您可以将第一个数组的元素复制到结果中,然后从第二个数组中添加金额。

$finalResult = [];
foreach ($array1 as $arr) {
    $finalResult[$arr['ID']] = $arr;
}
foreach ($array2 as $row) {
    $id = $row['ID'];
    unset($row['ID']);
    foreach ($row as $col => $value) {
        if (empty($finalResult[$id][$col])) {
            $finalResult[$id][$col] = $value;
        } else {
            $finalResult[$id][$col] += $value;
        }
    }
}

答案 1 :(得分:1)

$arr2 = array_column($array2, "ID");

$finalArray = array();
foreach($array1 as $arr){

    //Get the index of ID which exist in array2. And store in search variable.
    $search = array_keys($arr2, $arr['ID']);

    if(!$search){
        //If ID not exit in array-2, get the index of that ID and store in search variable. 
         $key = array_search(0, $arr2);
         $search[0] = $key;
         unset($array2[$key]['ID']);
    }

    //Fetch search item and merge.
    foreach($search as $value){
        $finalArray[] =  array_merge($arr,$array2[$value]);
    }
}

echo "<pre>";
print_r($finalArray);