How to remove all empty/null values in an array using PHP

H

How Can We Help?

How to remove all empty/null values in an array using PHP

  1. If you have an array with empty values and you wish to remove the empty valuesyou can use the following one line code
    		$array = array('','1','','3');
     
    		$array = array_filter($array, 'strlen');
     
     
     
    		echo '<pre>';
     
    		print_r($array);
     
    		echo '</pre>';
    
  2. The result will be only the NOT NULL values
    Array
    (
        [1] => 1
        [3] => 3
    )

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me