3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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, null, 'yearmonth'); $result = []; foreach ($dates as $date) { $result[] = $lookup[$date['yearmonth']] ?? ['count' => 0] + $date; } var_export($result);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
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', ), )

preferences:
137.5 ms | 408 KiB | 5 Q