Q:

Write a PHP script to remove all white spaces in an array

0

Write a PHP script to remove all white spaces in an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?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);
?>

Sample Output:

Array                                                       
(                                                           
    [0] => 15                                               
    [1] =>                                                  
    [2] =>                                                  
    [3] => -2                                               
    [4] =>                                                  
    [5] =>                                                  
    [6] =>                                                  
                                                            
    [7] => Red                                              
    [8] => 54                                               
    [9] =>                                                  
)                                                           
Array                                                       
(                                                           
    [0] => 15                                               
    [3] => -2                                               
    [7] => Red                                              
    [8] => 54                                               
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now