- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to trim all strings in an array in PHP ?
To trim all strings in an array in PHP, 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
"; } $result = array_map('trim', $arr); echo "
Updated Array...
"; foreach( $result 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 Updated Array... Value = John Value = Jacob Value = Tom Value = Tim
Example
Let us now see another example −
<?php $arr = array( " Kevin ", "Katie ", " Angelina ", " Jack "); echo "Array with leading and trailing whitespaces...
"; foreach( $arr as $value ) { echo "Value = $value
"; } array_walk($arr, create_function('&$val', '$val = trim($val);')); echo "
Updated Array...
"; foreach($arr as $key => $value) print($arr[$key] . "
"); ?>
Output
This will produce the following output−
Array with leading and trailing whitespaces... Value = Kevin Value = Katie Value = Angelina Value = Jack Updated Array... Kevin Katie Angelina Jack
- Related Articles
- trim() function in PHP
- concat(), replace(), and trim() Strings in Java.
- Finding all the longest strings from an array in JavaScript
- PHP How to add backslash inside array of strings?
- How to create an array of strings in JavaScript?
- Trim off '?' from strings in JavaScript
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Return all dates between two dates in an array in PHP
- How to re-index an array in PHP?
- How to convert an array to SimpleXML in PHP?
- How to trim a string using $.trim() in jQuery?
- Trim and split string to form array in JavaScript
- All ways to divide array of strings into parts in JavaScript
- Convert object to an array in PHP.
- In PHP, how can I add an object element to an array?

Advertisements