PHP array_replace
Apply a set of weekend overrides to the normal opening schedule.
A simple example
array_replace
- Start with A = 1, B = 2
- Do this Merge B = 3 and C = 4
- Result A = 1, B = 3, C = 4
Syntax
array_replace($hours, $overrides)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpApply weekend opening hours
A library keeps standard opening hours for every day, then receives a short list of weekend changes. The scheduler applies those overrides while preserving weekdays that the weekend list never mentions.
<?php
$hours = ['Mon' => 9, 'Sat' => 9];
$revised = array_replace($hours, ['Sat' => 10, 'Sun' => 11]);
echo $revised['Sat'];
10
The Saturday override changes the displayed hour to ten.
Merge weekend opening hours
The branch receives an updated Saturday timetable. A later set of assignments is merged into the first. Which value does the overlapping key hold?
Merge a volunteer rota
The organizer imports a replacement Saturday volunteer. A later set of assignments is merged into the first. Which value does the overlapping key hold?
What to watch for
array_replace returns a new array. The array union operator gives precedence to the left array instead.