3 回答

TA貢獻1811條經驗 獲得超6個贊
$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']. 這是一種合適的合并技術,因為鍵對于每個子數組來說都是唯一的且關聯的。

TA貢獻1936條經驗 獲得超7個贊
永遠不要低估能夠在 php 數組中以非常靈活的方式/方式使用鍵的力量。
<?php
//Copied from mickmackusa
$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'],
];
//End copy
/*
Make the keys of $jobs be the same as values
of yearmonth: (This makes it easy to compare later on)
Array
(
[0] => Array
(
[count] => 3
[yearmonth] => 2019-7
)
[1] => Array
(
[count] => 3
[yearmonth] => 2019-9
)
[2] => Array
(
[count] => 5
[yearmonth] => 2019-10
)
[2019-7] => Array
(
[count] => 3
[yearmonth] => 2019-7
)
[2019-9] => Array
(
[count] => 3
[yearmonth] => 2019-9
)
[2019-10] => Array
(
[count] => 5
[yearmonth] => 2019-10
)
)
*/
foreach($jobs as $key=>$item) {
$jobs[$item['yearmonth']] = $item;
}
//Create a third array $third_arr based on your $jobs and $dates array
$third_arr = [];
foreach($dates as $item) {
$key_value = $item['yearmonth'];
if (isset($jobs[$key_value]['yearmonth'])) {
//Available in dates and present in $jobs
//Just copy values from the $jobs item which relates to this yearmonth
$third_arr[] = $jobs[$key_value];
}
else {
//Available in dates but not present in $job
$third_arr[] = ['count'=>0, 'yearmonth'=>$key_value];
}
}
第三個數組的輸出$third_arr:
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
)
)
上面代碼的更壓縮版本如下所示:
foreach($jobs as $key=>$item) {
$jobs[$item['yearmonth']] = $item;
}
$third_arr = [];
foreach($dates as $item) {
$kvalue = $item['yearmonth'];
isset($jobs[$kvalue]['yearmonth']) ?
$third_arr[] = $jobs[$kvalue] : $third_arr[] = ['count'=>0, 'yearmonth'=>$kvalue];
}

TA貢獻1786條經驗 獲得超11個贊
因此,如果您添加count => 0到$date數組和索引,yearmonth您可以索引$job并將yearmonth其合并到$date:
$result = array_merge(array_column(array_map(function($v) { $v['count'] = 0; return $v; },
$date), null, 'yearmonth'),
array_column($job, null, 'yearmonth'));
- 3 回答
- 0 關注
- 210 瀏覽
添加回答
舉報