views:

79

answers:

3

How to trim a PHP array and remove all empty indexes

Array
(
    [0] => 
    [1] =>
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] => 4
    [8] => 6
    [9] =>

)

Output should be like

 Array
    (
        [0] => 4
        [1] => 6   
    )
+5  A: 

You are looking for the array_filter function ;-)


For instance, this portion of code :

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr);
var_dump($arr_filtered);

Will give you the following output :

array
  7 => int 4
  8 => int 6

Note that all "falsy" values have been removed.


And if you want to be more specific, you can specify your own filtering function. For instance, to remove only nulls from the array, I could use this :

function my_filter($item) {
    if ($item === null) {
        return false;
    }
    return true;
}

$arr = array(null, 0, null, 0, '', null, '', 4, 6, '', );
$arr_filtered = array_filter($arr, 'my_filter');
var_dump($arr_filtered);

And I'd get :

array
  1 => int 0
  3 => int 0
  4 => string '' (length=0)
  6 => string '' (length=0)
  7 => int 4
  8 => int 6
  9 => string '' (length=0)
Pascal MARTIN
Note: without an explicit callback, values of 0, "0" and NULL will also be filtered.
LiraNuna
@LiraNuna : I've edited my answer to an example that illustrates that ; thanks for the note :-)
Pascal MARTIN
And to remove only nulls one should define callback function. This is where lambda style function fits.`array_filter($arr, create_function('$v', 'return $v !== NULL ? TRUE : FALSE'));`
doc
`array_filter($arr, create_function('$v', 'return $v !== NULL'));` Is enough :)
LiraNuna
Yes, you could do that using `create_function` -- but I definitly don't like `create_function` : it's not good for performances *(especially if called a lot of times)*, and it's hard to write and read, as all the code is in a string *(which means no syntax highlighting, and an escaping hell)* ;;; instead, I'd love using the anonymous functions that have been added in PHP 5.3 ;-)
Pascal MARTIN
+1  A: 

Sounds like homework to me.

I'd suggest you take a look at the array_filter function. That seems to be the most appropriate option.

Dancrumb
A: 

Here another way:

<?php

$array = array(
    0 => 0,
    1 => ,
    2 => '',
    3 => 4,
    4 => 6,
    5 => null
);

foreach( $array as $a )
{
    if( !empty($a) AND $a != NULL AND $a != 0 ) // NULL, 0
    {
        $new_array[] = $a;
    }
}

print_r( $new_array );

?>

Output will be:

Array
(
    [0] => 4
    [1] => 6
)
ahmet2106
You don't need to check if array is empty inside foreach loop. Result of operator `!=` in case of a = 0 or a = NULL will be the same. You definitely should allow 'null' strings. You are probably young developer, keep going but please correct or delete this post.
doc
@doc Thanks first of all. Yes i'm young, but this does not mean anything. It is so like he wants it, yes my first version only checked null and '', but i think maybe he wants this too, need not to be, he can change the if() like he wants it, this was just an idea... Does my code do what he want? Yes it does... If you want you can edit it :)
ahmet2106