1

I have this array, and I need to delete the empty value and just keep the other values.

Array
(
    [12] => Array
        (
            [0] => 12
            [1] =>  Philippines
            [2] => 94,013,200
            [3] => Mid-2010
            [4] => 0.0136
        )
    [13] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
        )
Issam Mousleh
  • 103
  • 1
  • 3
  • 8

2 Answers2

2

You can use array_map and array_filter functions for removing empty values from multi-dimensional array.

Solution:

$array = array_filter(array_map('array_filter', $yourArr));

Example:

$yourArr[12] = array('12','Philippines');
$yourArr[13] = array('','');
$array = array_filter(array_map('array_filter', $yourArr));

echo "<pre>";
print_r($array);

Result:

Array
(
    [12] => Array
        (
            [0] => 12
            [1] => Philippines
        )

)
devpro
  • 16,184
  • 3
  • 27
  • 38
1

Use array_map() and array_filter()

$result = array_map('array_filter', $a)

array_filter() removes blank elements from array in this case.

array_map() function calls a function on every array element, in this cause, it calls array_filter() and removes empty elements.

Working Code:

<?php
$a = array(12 => array(12, 'Philippines', '94,013,200', 'Mid-2010', '0.0136'), 13 => array('', '', '', '', ''));
$result = array_map('array_filter', $a);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Pupil
  • 23,834
  • 6
  • 44
  • 66