Delete an element with an empty value in the array

Array ( [0] => 0 //value is int 0 which isn;t empty value [1] => //this is empty value [2] => //this is empty value ) 

I would like to make the above array as follows: Can someone help me?

Many thanks

 Array ( [0] => 0 ) 
+8
php
source share
5 answers

You can use array_filter to remove an empty value (null, false, '', 0):

 array_filter($array); 

If you don't want to remove 0 from your array, see @Sabari answer:

 array_filter($array,'strlen'); 
+19
source share

You can use:

Only to remove NULL values:

 $new_array_without_nulls = array_filter($array_with_nulls, 'strlen'); 

To remove false values:

 $new_array_without_nulls = array_filter($array_with_nulls); 

Hope this helps :)

+5
source share
 array_filter($array, function($var) { //because you didn't define what is the empty value, I leave it to you return !is_empty($var); }); 
+1
source share

This is a typical case for array_filter . First you need to define a function that returns TRUE if the value should be stored and FALSE if it should be deleted:

 function preserve($value) { if ($value === 0) return TRUE; return FALSE; } $array = array_filter($array, 'preserve'); 

Then you specify in the callback function (here preserve ) what is empty and what is not. You have not specifically asked your question, so you need to do it yourself.

0
source share

quick way to find numbers also Zero (0)

  var_dump( array_filter( array('0',0,1,2,3,'text') , 'is_numeric' ) ); /* print : array (size=5) 0 => string '0' (length=1) 1 => int 0 2 => int 1 3 => int 2 4 => int 3 */ 
0
source share

All Articles