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

我有两个这样的数组:


数组1


$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'

        )


);

阵列2


$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);


但是因为在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 添加行。如何修改我当前的代码,以便它为我提供所需的输出?


狐的传说
浏览 177回答 2
2回答

回首忆惘然

$arr2 = array_column($array2, "ID");$finalArray = array();foreach($array1 as $arr){&nbsp; &nbsp; //Get the index of ID which exist in array2. And store in search variable.&nbsp; &nbsp; $search = array_keys($arr2, $arr['ID']);&nbsp; &nbsp; if(!$search){&nbsp; &nbsp; &nbsp; &nbsp; //If ID not exit in array-2, get the index of that ID and store in search variable.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$key = array_search(0, $arr2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$search[0] = $key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;unset($array2[$key]['ID']);&nbsp; &nbsp; }&nbsp; &nbsp; //Fetch search item and merge.&nbsp; &nbsp; foreach($search as $value){&nbsp; &nbsp; &nbsp; &nbsp; $finalArray[] =&nbsp; array_merge($arr,$array2[$value]);&nbsp; &nbsp; }}echo "<pre>";print_r($finalArray);

万千封印

使结果数组成为以 ID 为键的关联数组。您可以将第一个数组的元素复制到结果中,然后添加第二个数组中的数量。$finalResult = [];foreach ($array1 as $arr) {&nbsp; &nbsp; $finalResult[$arr['ID']] = $arr;}foreach ($array2 as $row) {&nbsp; &nbsp; $id = $row['ID'];&nbsp; &nbsp; unset($row['ID']);&nbsp; &nbsp; foreach ($row as $col => $value) {&nbsp; &nbsp; &nbsp; &nbsp; if (empty($finalResult[$id][$col])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finalResult[$id][$col] = $value;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finalResult[$id][$col] += $value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP