PHP Delete Array Items

0
3K

Remove Array Item

To remove an existing item from an array, you can use the array_splice() function.

With the array_splice() function you specify the index (where to start) and how many items you want to delete.

ExampleGet your own PHP Server

Remove the second item:

$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 1);
Try it Yourself »

After the deletion, the array gets reindexed automtically, starting at index 0.


Using the unset Function

You can also use the unset() function to delete existing array items.

Note: The unset() function does not re-arrange the indexes, meaning that after deletion the array will no longer contain the missing indexes.

Example

Remove the second item:

$cars = array("Volvo", "BMW", "Toyota");
unset($cars[1]);
Try it Yourself »

Remove Multiple Array Items

To remove multiple items, the array_splice() function takes a length parameter that allows you to specify the number of items to delete.

Example

Remove 2 items, starting a the second item (index 1):

$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 2);
Try it Yourself »

The unset() function takes a unlimited number of arguments, and can therefor be used to delete multiple array items:

Example

Remove the first and the second item:

$cars = array("Volvo", "BMW", "Toyota");
unset($cars[0], $cars[1]);
Try it Yourself »

Remove Item From an Associative Array

To remove items from an associative array, you can use the unset() function.

Specify the key of the item you want to delete.

Example

Remove the "model":

$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
unset($cars["model"]);
Try it Yourself »

Using the array_diff Function

You can also use the array_diff() function to remove items from an associative array.

This function returns a new array, without the specified items.

Example

Create a new array, without "Mustang" and "1964":

$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
$newarray = array_diff($cars, ["Mustang", 1964]);
Try it Yourself »

Note: The array_diff() function takes values as parameters, and not keys.


Remove the Last Item

The array_pop() function removes the last item of an array.

Example

Remove the last item:

$cars = array("Volvo", "BMW", "Toyota");
array_pop($cars);
Try it Yourself »

Remove the First Item

The array_shift() function removes the first item of an array.

Example

Remove the first item:

$cars = array("Volvo", "BMW", "Toyota");
array_shift($cars);
Try it Yourself »
Cerca
Categorie
Leggi tutto
Altre informazioni
PHP Arrays
An array stores multiple values in one single variable: ExampleGet your own PHP Server $cars...
By PHP Tutorial 2024-05-17 07:53:31 0 2K
Altre informazioni
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
By PHP Tutorial 2024-05-17 08:06:46 0 3K
Altre informazioni
PHP if...else Statements
PHP - The if...else Statement The if...else statement executes some code if a...
By PHP Tutorial 2024-05-17 07:46:12 0 2K
Altre informazioni
PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP. PHP Loops...
By PHP Tutorial 2024-05-17 07:48:35 0 2K
Altre informazioni
PHP do while Loop
The do...while loop - Loops through a block of code once, and then repeats the loop as...
By PHP Tutorial 2024-05-17 07:49:22 0 2K