$items = [
1 => [
"hours" => 6
],
2 => [
"hours" => 4
],
3 => [
"hours" => 18
],
4 => [
"hours" => 12
],
5 => [
"hours" => 8
]
];
$items = [
1 => [
"hours" => 6,
"sub_hours" => 0
],
2 => [
"hours" => 4,
"sub_hours" => 0
],
3 => [
"hours" => 18,
"sub_hours" => 0
],
4 => [
"hours" => 12,
"sub_hours" => 0
],
5 => [
"hours" => 8,
"sub_hours" => 0
]
];
array_map
или array_walk
.$limit = 8;
// VAR 1
$processedItems = \array_map(function ($value) use ($limit) {
$subHours = 0;
$hours = $value['hours'];
if ($hours > $limit) {
$subHours = $hours - $limit;
$hours = $limit;
}
return [
'hours' => $hours,
'sub_hours' => $subHours,
];
}, $items);
// VAR 2
\array_walk($items, function (&$value) use ($limit) {
$hours = $value['hours'];
if ($hours > $limit) {
$value['hours'] = $limit;
$value['sub_hours'] = $hours - $limit;
}
});