How to remove all 0’s (zeros) from an PHP array

H

How Can We Help?

If I have the following PHP array

$array = array(9,0,6,4,5,0,9,0,0,0);

and I need to have all 0’s removed from it.

Use the PHP array_filter function. If you don’t supply a callback function it will filter out all values that are equal to false

$new_array = array_filter($array);

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