
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to delete an element from an array in PHP and re-index the array?
The ‘unset’ function can be used to remove an element from the array and use the ‘array_values’ function that resets the indices of the array.
Example
<?php $my_arr = array( 'this', 'is', 'a', 'sample', 'only'); echo"The array is "; var_dump($my_arr); unset($my_arr[4]); echo"The array is now "; $my_arr_2 = array_values($my_arr); var_dump($my_arr_2); ?>
Output
The array is array(5) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(1) "a" [3]=> string(6) "sample" [4]=> string(4) "only" } The array is now array(4) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(1) "a" [3]=> string(6) "sample" }
An array is declared that contains string values. The array is displayed and the ‘unset’ function is used to delete a specific index element from the array. Then the array is displayed again to reflect the changes on the console.
- Related Articles
- How to re-index an array in PHP?
- How to delete element from an array in MongoDB?
- How to delete an array element based on key in PHP?
- PHP program to delete an element from the array using the unset function
- Removing Array Element and Re-Indexing in PHP
- How to delete/remove an element from a C# array?
- How to delete elements from an array?
- Delete an element from array using two traversals and one traversal in C++?
- In PHP, how can I add an object element to an array?
- How to access an associative array by integer index in PHP?
- How to get the first element of an array in PHP?
- How to remove an element from an array in Java
- How to remove an array element by its index in MongoDB?
- C program to delete an array element using Pointers
- Delete an element from array using two traversals and one traversal in C++ program

Advertisements