Unset array key if value is null

Hello forums

How do you unset or remove an array key if it has no value example:

Array ( [0] => [1] => egg [2] => bacon [3]=>)

will be:

array([0]=>egg [1]=> bacon)

Thanks

I believe this would work:


$test_array = array(0 => '',1 => 'egg',2 => 'bacon',3 => '');

//Show values in filtered array
var_dump(array_filter($test_array));


ok I got using array_values();

Thanks !

Thanks Sillysoft it removed the the keys with no values but retained the the original keys with the ones with values.

array([1]=>egg, [2]=>bacon)

How do we re index the keys?

array([0]=>egg [1]=> bacon)

thanks

Someone will do this better than me, but here’s a way to do it off the top of my head.


foreach($array AS $value) {
 if(!isempty($value)) {
   $out[] = $value;
 }
}
$array = $out;