<?php $a = ['2018-05'=> '15', '2018-06'=> '100']; $b = ['2018-05'=> '5']; $years = array_map(NULL, $a, $b); $years = array_combine(array_keys($a), $years); $differences = array_map(function ($year_diffs) : float { // Did we get a pair to compare? $has_diff = isset($year_diffs[0]) and isset($year_diffs[1]); // If we got a pair then compare, else assume we got one. $diff = $has_diff ? abs($year_diffs[0] - $year_diffs[1]) : $year_diffs[0]; return $diff; }, $years); var_dump($differences);
You have javascript disabled. You will not be able to edit any code.