

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing Array Element and Re-Indexing in PHP
To remove an array element and re-index the array, the code is as follows−
Example
<?php $arr = array( " John ", "Jacob ", " Tom ", " Tim "); echo "Array with leading and trailing whitespaces...\n"; foreach( $arr as $value ) { echo "Value = $value \n"; } echo "\nComma separated list...\n"; print_r(implode(', ', $arr)); $result = array_map('trim', $arr); echo "\nUpdated Array...\n"; foreach( $result as $value ) { echo "Value = $value \n"; } unset($result[1]); echo "\nUpdated Array...Re-indexed\n"; $result2 = array_values($result); foreach( $result2 as $value ) { echo "Value = $value \n"; } ?>
Output
This will produce the following output−
Array with leading and trailing whitespaces... Value = John Value = Jacob Value = Tom Value = Tim Comma separated list... John , Jacob , Tom , Tim Updated Array... Value = John Value = Jacob Value = Tom Value = Tim Updated Array...Re-indexed Value = John Value = Tom Value = Tim
- Related Questions & Answers
- How to delete an element from an array in PHP and re-index the array?
- Removing empty array elements in PHP
- How to re-index an array in PHP?
- Array Indexing and Printing in Python (pyCharm)
- Removing duplicate elements from an array in PHP
- Removing an element from an Array in Javascript
- Removing an array element from MongoDB collection using update() and $pull
- Removing an array element from a MongoDB collection
- Adding and Removing Elements in Perl Array
- Removing an element from the end of the array in Javascript
- Removing an element from the start of the array in javascript
- Removing an element from a given position of the array in Javascript
- Removing _id element from PyMongo results?
- Removing the Min Element from Deaps
- Search for PHP array element containing string?
Advertisements