PHP array_splice for removal
Remove the item at a chosen position from a packing list.
A simple example
array_splice for removal
- Start with A, B, C
- Do this Remove the middle item
- Result A, C
Syntax
array_splice($items, $index, 1)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpTake a cancelled item off a packing list
A customer cancels the second item on an order after the packing list has printed. The picker uses its position to remove exactly that item and reads the removed label back to the customer.
<?php
$items = ['Tea', 'Soap', 'Rice'];
$removed = array_splice($items, 1, 1);
echo $removed[0];
Soap
Soap is the item removed from the second position.
Prepare the next workshop roster
A teacher reads a workshop roster in arrival order. The middle entry cancels and is removed from the plan. Which two entries still appear, in order?
Work from the delivery round
A courier has three labelled stops in route order. The middle entry cancels and is removed from the plan. Which two entries still appear, in order?
What to watch for
Splice returns an array of removed items. An empty result means no item was removed.