array_splice() FunctionThe array_splice() function in PHP is used to remove a portion of an array and optionally replace it with another array. It is useful when you need to modify an array by removing, replacing, or inserting elements at a specific position.
// General syntax of array_splice in PHP
array_splice($array,$start,$length,$replacement);
// Remove two elements starting from index 2
The elements Cherry and Date are removed, leaving Apple, Banana, and Elderberry.
// Replace two elements with new values
Cherry and Date are replaced by Grapes and Kiwi at the same position.
// Remove second last element using negative index
// Insert elements without removing any existing items
Current Array: ["Red", "Green", "Blue", "Yellow", "Purple"]
Imagine an array as a row of boxes. array_splice() cuts boxes from a chosen position and optionally inserts new boxes at the same location — all in one operation.
array_splice() when you need multiple changes at once0 for pure insertion