3 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
$jobs
要在迭代數(shù)組時(shí)有效地搜索數(shù)組中的日期$dates
,請(qǐng)創(chuàng)建一個(gè)“查找”數(shù)組。
查找應(yīng)該具有代表關(guān)系數(shù)據(jù)的鍵 ( yearmonth
)。
使用數(shù)組中遇到的每個(gè)日期$dates
,檢查該yearmonth
值是否表示為查找中的鍵 - 如果是,則使用該count
值,如果不是,則設(shè)置 0。
&
變量之前的意思是“通過引用修改——這樣原始數(shù)組就會(huì)發(fā)生變化,而不需要聲明一個(gè)新的輸出數(shù)組。
??
是“空合并運(yùn)算符”,這允許在變量未聲明時(shí)使用后備值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,
),
)
或者,如果您要求聲明一個(gè)新的輸出數(shù)組,并且關(guān)聯(lián)子數(shù)組的順序必須與您的問題中的順序相同,那么這是我對(duì)相同技術(shù)的調(diào)整:
代碼:(演示)
$lookup = array_column($jobs, null, 'yearmonth');
$result = [];
foreach ($dates as $date) {
$result[] = $lookup[$date['yearmonth']] ?? ['count' => 0] + $date;
}
var_export($result);
查找數(shù)組現(xiàn)在包含完整的子數(shù)組數(shù)據(jù)。 array_column()用作null第二個(gè)參數(shù)來防止隔離單行,yearmonth用作第三個(gè)參數(shù)來分配第一級(jí)鍵。
foreach 循環(huán)不再“通過引用修改”。空合并運(yùn)算符仍然用于避免調(diào)用isset(),并且非常適合簡潔地編寫后備數(shù)據(jù)。count我在包含元素的硬編碼數(shù)組和數(shù)組之間使用“聯(lián)合運(yùn)算符” $date——避免調(diào)用array_merge()或手動(dòng)編寫['yearmonth' => $date['yearmonth']. 這是一種合適的合并技術(shù),因?yàn)殒I對(duì)于每個(gè)子數(shù)組來說都是唯一的且關(guān)聯(lián)的。

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
永遠(yuǎn)不要低估能夠在 php 數(shù)組中以非常靈活的方式/方式使用鍵的力量。
<?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];
}
}
第三個(gè)數(shù)組的輸出$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貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
因此,如果您添加count => 0到$date數(shù)組和索引,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 關(guān)注
- 221 瀏覽
添加回答
舉報(bào)