Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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...
"; foreach( $arr as $value ) { echo "Value = $value
"; } echo "\nComma separated list...
"; print_r(implode(', ', $arr)); $result = array_map('trim', $arr); echo "\nUpdated Array...
"; foreach( $result as $value ) { echo "Value = $value
"; } unset($result[1]); echo "\nUpdated Array...Re-indexed
"; $result2 = array_values($result); foreach( $result2 as $value ) { echo "Value = $value
"; } ?>
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
Advertisements
