How to Remove All Whitespace from Array in PHP?

Dec 05, 2022

.

Admin

How to Remove All Whitespace from Array in PHP?

Hi Dev,

This tutorial will give you example of how to remove whitespace from array in php?. I’m going to show you about remove whitespace from array in php example. This tutorial will give you simple example of php remove whitespace from array. We will use how to use function to remove whitespace from array in php?

in this example to remove whitespace from array in PHP. in this example, we will use to array_filter() and array_map() function used to remove whitespace. so Let's see the following example with output.

Example: Remove Whitespace in Array


index.php

<?php

$myArray = array(' apple', 'orange ', ' banana', ' ', '', 'strawberries ');

// Remove All Whitespace in Array

$newArray = array_filter(array_map('trim', $myArray));

print_r($newArray);

?>

Output:

Array

(

[0] => apple

[1] => orange

[2] => banana

[5] => strawberries

)

I hope it could help you...

#PHP