PHP Remove Array Items
PHP has several functions for taking items out of an array, each suited to a different situation:
array_splice()- takes out a stretch of items, given a start position and a lengthunset()- deletes the element stored under a specific keyarray_diff()- filters items out of an associative arrayarray_pop()- drops the last itemarray_shift()- drops the first item
PHP array_splice() Function
array_splice() needs to know two things: the index to start at and the number of items to delete.
Once the items are gone, PHP renumbers what is left, so the indices run from 0 again without gaps.
Remove one item with array_splice()
Delete one item at index 1, the second car:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 1);
var_dump($cars);
?>
Remove Multiple Array Items
Deleting several items with array_splice() is a matter of its length parameter, which sets how many items go.
Remove multiple items with array_splice()
Delete 2 items, beginning at index 1 (the second item):
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 2);
var_dump($cars);
?>
PHP unset() Function
unset() is another way to delete items that exist in an array.
Note: Unlike array_splice(), it leaves the numbering alone. Delete the element at index 1 and the others stay at 0, 2, 3 and so on, so a gap appears in the indices.
Remove one item with unset()
Delete the second item and notice the gap in the output:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
unset($cars[1]);
var_dump($cars);
?>
Remove Multiple Array Items with unset()
Since unset() accepts as many arguments as you like, one call can delete several items:
Remove multiple items with unset()
Delete the items at index 0 and index 1 together:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
unset($cars[0], $cars[1]);
var_dump($cars);
?>
Remove Item From an Associative Array
unset() also works on associative arrays.
Just pass it the key of the item that should go.
Remove associative array item with unset()
Delete the "model" item:
Output: PHP script
<?php
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
unset($cars["model"]);
var_dump($cars);
?>
PHP array_diff() Function
Another option for associative arrays is array_diff().
It leaves the original array untouched and returns a new one that lacks the items you listed.
Note: You list values here, not keys.
Remove items with array_diff()
Build a new array that leaves out "Mustang" and "1964":
Output: PHP script
<?php
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
$newarray = array_diff($cars, ["Mustang", 1964]);
var_dump($newarray);
?>
PHP array_pop() - Remove Last Array Item
To drop the final item of an array, call array_pop().
Remove last item with array_pop()
Drop "Toyota", the last car:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
array_pop($cars);
var_dump($cars);
?>
PHP array_shift() - Remove First Array Item
To drop the item at the very start of an array, call array_shift().
Remove first item with array_shift()
Drop "Volvo", the first car:
Output: PHP script
<?php
$cars = array("Volvo", "BMW", "Toyota");
array_shift($cars);
var_dump($cars);
?>