当值相同时,将 2 个数组合并到第三个新数组中

我有一个名为 job counts 的数组,如下$job =>


Array

        (

            [0] => Array([count] => 3[yearmonth] => 2019-7)

        

            [1] => Array([count] => 3[yearmonth] => 2019-9)

        

            [2] => Array([count] => 5[yearmonth] => 2019-10))           

    )

第二个日期数组为$date =>


Array

    ([0] => Array([yearmonth] => 2019-6)

    

     [1] => Array([yearmonth] => 2019-7)

    

     [2] => Array([yearmonth] => 2019-8)

    

     [3] => Array([yearmonth] => 2019-9)

    

     [4] => Array([yearmonth] => 2019-10)

)

作业数组不是连续数组。所以这里我想要的是,如果一个条目在 $date 中可用但在 $job 中不存在,则创建第三个数组,其中 count = 0 和yearmonth 值。像下面这样的东西


Array

    ([0] => Array([count] => 0[yearmonth] => 2019-6)

    

     [1] => Array([count] => 3[yearmonth] => 2019-7)

    

     [2] => Array([count] => 0[yearmonth] => 2019-8)

    

     [3] => Array([count] => 3[yearmonth] => 2019-9)

    

     [4] => Array([count] => 5[yearmonth] => 2019-10)

)


白猪掌柜的
浏览 166回答 3
3回答

杨魅力

$jobs要在迭代数组时有效地搜索数组中的日期$dates,请创建一个“查找”数组。查找应该具有代表关系数据的键 ( yearmonth)。使用数组中遇到的每个日期$dates,检查该yearmonth值是否表示为查找中的键 - 如果是,则使用该count值,如果不是,则设置 0。&变量之前的意思是“通过引用修改——这样原始数组就会发生变化,而不需要声明一个新的输出数组。??是“空合并运算符”,这允许在变量未声明时使用后备值null。代码:(演示)$jobs = [    ['count' => 3, 'yearmonth' => '2019-7'],    ['count' => 3, 'yearmonth' => '2019-9'],    ['count' => 5, 'yearmonth' => '2019-10'],         ];$dates = [    ['yearmonth' => '2019-6'],    ['yearmonth' => '2019-7'],    ['yearmonth' => '2019-8'],    ['yearmonth' => '2019-9'],    ['yearmonth' => '2019-10'],];$lookup = array_column($jobs, 'count', 'yearmonth');foreach ($dates as &$date) {    $date['count'] = $lookup[$date['yearmonth']] ?? 0;}var_export($dates);输出:array (  0 =>   array (    'yearmonth' => '2019-6',    'count' => 0,  ),  1 =>   array (    'yearmonth' => '2019-7',    'count' => 3,  ),  2 =>   array (    'yearmonth' => '2019-8',    'count' => 0,  ),  3 =>   array (    'yearmonth' => '2019-9',    'count' => 3,  ),  4 =>   array (    'yearmonth' => '2019-10',    'count' => 5,  ),)或者,如果您要求声明一个新的输出数组,并且关联子数组的顺序必须与您的问题中的顺序相同,那么这是我对相同技术的调整:代码:(演示)$lookup = array_column($jobs, null, 'yearmonth');$result = [];foreach ($dates as $date) {    $result[] = $lookup[$date['yearmonth']] ?? ['count' => 0] + $date;}var_export($result);查找数组现在包含完整的子数组数据。 array_column()用作null第二个参数来防止隔离单行,yearmonth用作第三个参数来分配第一级键。foreach 循环不再“通过引用修改”。空合并运算符仍然用于避免调用isset(),并且非常适合简洁地编写后备数据。count我在包含元素的硬编码数组和数组之间使用“联合运算符” $date——避免调用array_merge()或手动编写['yearmonth' => $date['yearmonth']. 这是一种合适的合并技术,因为键对于每个子数组来说都是唯一的且关联的。

LEATH

永远不要低估能够在 php 数组中以非常灵活的方式/方式使用键的力量。<?php//Copied from mickmackusa$jobs = [&nbsp; &nbsp; ['count' => 3, 'yearmonth' => '2019-7'],&nbsp; &nbsp; ['count' => 3, 'yearmonth' => '2019-9'],&nbsp; &nbsp; ['count' => 5, 'yearmonth' => '2019-10'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];$dates = [&nbsp; &nbsp; ['yearmonth' => '2019-6'],&nbsp; &nbsp; ['yearmonth' => '2019-7'],&nbsp; &nbsp; ['yearmonth' => '2019-8'],&nbsp; &nbsp; ['yearmonth' => '2019-9'],&nbsp; &nbsp; ['yearmonth' => '2019-10'],];//End copy/*&nbsp; &nbsp; Make the keys of $jobs be the same as values&nbsp; &nbsp; of yearmonth: (This makes it easy to compare later on)&nbsp; &nbsp; Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [2019-7] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [2019-9] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; [2019-10] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; )*/foreach($jobs as $key=>$item) {&nbsp; &nbsp; $jobs[$item['yearmonth']] = $item;}//Create a third array $third_arr based on your $jobs and $dates array$third_arr = [];foreach($dates as $item) {&nbsp; &nbsp; $key_value = $item['yearmonth'];&nbsp; &nbsp; if (isset($jobs[$key_value]['yearmonth'])) {&nbsp; &nbsp; &nbsp; &nbsp; //Available in dates and present in $jobs&nbsp; &nbsp; &nbsp; &nbsp; //Just copy values from the $jobs item which relates to this yearmonth&nbsp; &nbsp; &nbsp; &nbsp; $third_arr[] = $jobs[$key_value];&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; //Available in dates but not present in $job&nbsp; &nbsp; &nbsp; &nbsp; $third_arr[] = ['count'=>0, 'yearmonth'=>$key_value];&nbsp; &nbsp; }}第三个数组的输出$third_arr:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-6&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-7&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-8&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-9&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [4] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [count] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [yearmonth] => 2019-10&nbsp; &nbsp; &nbsp; &nbsp; ))上面代码的更压缩版本如下所示:foreach($jobs as $key=>$item) {&nbsp; &nbsp; $jobs[$item['yearmonth']] = $item;}$third_arr = [];foreach($dates as $item) {&nbsp; &nbsp; $kvalue = $item['yearmonth'];&nbsp; &nbsp; isset($jobs[$kvalue]['yearmonth']) ?&nbsp;&nbsp; &nbsp; $third_arr[] = $jobs[$kvalue] : $third_arr[] = ['count'=>0, 'yearmonth'=>$kvalue];}

Qyouu

因此,如果您添加count => 0到$date数组和索引,yearmonth您可以索引$job并将yearmonth其合并到$date:$result = array_merge(array_column(array_map(function($v) { $v['count'] = 0; return $v; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$date), null, 'yearmonth'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_column($job, null, 'yearmonth'));
打开App,查看更多内容
随时随地看视频慕课网APP