Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Casting PHP Math PHP Constants PHP Magic Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals PHP RegEx

PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions

PHP OOP

PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Interfaces PHP Traits PHP Static Methods PHP Static Properties PHP Namespaces PHP Iterables

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP Examples

PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Certificate

PHP Reference

PHP Overview PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP Delete Array Items


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.

Example

Remove the second item:

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

After the deletion, the array gets reindexed automatically, 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 therefore 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 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.