将值的总和添加到PHP中第二个数组中每个匹配ID的第一个数组中

时间:2019-06-18 07:56:28

标签: 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);
           $array2[$key]['Found'] = "No";

        }
  else {
    $array2[$key]['Found'] = "Yes";
   }
        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 in array2
            [Found] => Yes
        )

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

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

)

但是由于在 array2 中有两个ID为101的条目,但是上面的代码仅对匹配ID进行了第一个匹配

预期输出为:

所需的输出

   Array
(
    [0] => Array
        (
            [ID] => 101
            [Code] => 1075
            [Date] => 2012-03-03 17:13:12.433
            [Amount] => 6890 //sum of all the amounts(1234+5656)for matching ID 101
            [Found] => Yes
        )
    [1] => Array
        (
            [ID] => 103
            [Code] => 175
            [Date] => 2012-09-05 20:30:02.217
            [Amount] => 1342
            [Found] => Yes
        )

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

)

在这里我不知道该怎么做。

该代码应为array2的每个匹配ID加上金额字段,然后将其金额合并到array1金额字段,如上面的预期输出所示。

如何修改当前代码,使其提供所需的输出?

1 个答案:

答案 0 :(得分:3)

那又怎么样:

$finalArray = array();
foreach ($array1 as $arr1)
{
    $amount = 0;
    foreach ($array2 as $key2 => $arr2)
    {
        if ($arr1['ID'] === $arr2['ID'])
        {
            $amount += $arr2['Amount'];
            unset($array2[$key2]);
        }
    }

    $finalArray[] = array_merge($arr1, array(
        'Amount'    => $amount,
        'Found'     => $amount ? "Yes" : "No"
    ));
}

print_r($finalArray);

输出

Array
(
    [0] => Array
        (
            [ID] => 101
            [Code] => 1075
            [Date] => 2012-03-03 17:13:12.433
            [amount] => 6890
            [found] => Yes
        )

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

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

)

如果您需要进一步说明,请告诉我。