3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data_json = '{ "EXPORT": [ { "dates": "2019-07-01", "c_job": 12 }, { "dates": "2019-07-02", "c_job": 8 }, { "dates": "2019-07-04", "c_job": 11 } ], "IMPORT": [ { "dates": "2019-07-03", "c_job": 11 } ] }'; $blank_json = '[ { "dates": "2019-07-01", "c_job": 0 }, { "dates": "2019-07-02", "c_job": 0 }, { "dates": "2019-07-03", "c_job": 0 }, { "dates": "2019-07-04", "c_job": 0 } ]'; $data = json_decode($data_json, true); $blank = json_decode($blank_json, true); $datafinal = array(); foreach ($data as $key => $value) { foreach ($blank as $bkey => $bvalue) { if (($dkey = array_search($bvalue['dates'], array_column($value, 'dates'))) !== false) { $datafinal[$key][$bkey] = $value[$dkey]; } else { $datafinal[$key][$bkey] = $bvalue; } } } echo json_encode($datafinal, JSON_PRETTY_PRINT);
Output for 7.1.25 - 7.1.31, 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
{ "EXPORT": [ { "dates": "2019-07-01", "c_job": 12 }, { "dates": "2019-07-02", "c_job": 8 }, { "dates": "2019-07-03", "c_job": 0 }, { "dates": "2019-07-04", "c_job": 11 } ], "IMPORT": [ { "dates": "2019-07-01", "c_job": 0 }, { "dates": "2019-07-02", "c_job": 0 }, { "dates": "2019-07-03", "c_job": 11 }, { "dates": "2019-07-04", "c_job": 0 } ] }

preferences:
165.08 ms | 409 KiB | 5 Q