PHP Array Exercises : Remove all white spaces in an array
PHP Array: Exercise-54 with Solution
Write a PHP script to remove all white spaces in an array.
<?php $my_array = array(15, null, " ", -2, NULL, "", " \n", "Red", 54, "\t"); print_r($my_array); $result = array_filter($my_array, create_function('$x','return preg_match("#\S#", $x);')); print_r($result); ?>
Array (  => 15  =>  =>  => -2  =>  =>  =>  => Red  => 54  => ) Array (  => 15  => -2  => Red  => 54 )
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP script to delete a specific value from an array using array_filter() function.
Next: Write a PHP function to convert a string to an array (trimming every line and remove empty lines).
What is the difficulty level of this exercise?
PHP: Tips of the Day
PHP: Extract numbers from a string
$str = 'In My Cart : 11 12 items'; preg_match_all('!\d+!', $str, $matches); print_r($matches);
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework