2

How do you remove the lines that have empty elements from a multidimensional-array in PHP?

For instance, from:

1: a, b, c, d
2: d, _, b, a
3: a, b, _, _
4: d, c, b, a
5: _, b, c, d
6: d, c, b, a

to

1: a, b, c, d
4: d, c, b, a
6: d, c, b, a

Thanks!

5 Answers 5

7
$arr = array(... your multi dimension array here ...);
foreach($arr as $idx => $row) {
    if (preg_grep('/^$/', $row)) {
        unset($arr[$idx]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use this code:

$source = array(
    array('a', 'b', 'c', 'd'),
    array('d', '_', 'b', 'a'),
    array('a', 'b', '_', '_'),
    array('d', 'c', 'b', 'a'),
    array('_', 'b', 'c', 'd'),
    array('d', 'c', 'b', 'a'),
);

$sourceCount = count($source);

for($i=0; $i<$sourceCount; $i++)
{
  if(in_array("_", $source[$i])) unset($source[$i]);
}

See: http://ideone.com/Vfd6Z

2 Comments

The one issue I have with this solution is the assumption that $source is an ordered, consistent array. For this example it works, but it is possible that the array has arbitrary keys, or else that is has previously been manipulated and has a key structure like {0,1,4,8}. To fix this you could use array_values() on $source both before and after the array manipulation. Using a foreach instead of a for loop would work around both these issues.
You're right. This code will also work with any type of key: ideone.com/smNR9
1

Loop through the multidimensional array and check to see if the array at position i contains any empty elements. If it does, call unset($arr[i]) to remove it.

for($i=0,$size=sizeof($arr); $i < $size; $i++) {
    if( in_array( "", $arr[$i] ) )
        unset( $arr[$i] );
}

Comments

1

I would iterate through a foreach loop myself something like:

<?php
    // Let's call our multidimensional array $md_array for this

    foreach ($md_array as $key => $array)
    {
        $empty_flag = false;

        foreach ($array as $key => $val)
        {
            if ($val == '')
            {
                $empty_flag = true;
            }
        }

        if ($empty_flag == true)
        {
            unset($md_array[$key]);
        }
    }
?>

There's almost definitely a more efficient way of doing this so anyone else who has a better solution feel free to let me and Alex know.

Comments

1

Try this:

Note: $arr is your array.

foreach ( $arr as $key => $line ) {

    foreach ( $line as $item ) {

        if ( empty( $item ) ) {

            unset( $arr[$key] );
            break;
        }

    }

}

Cheers

2 Comments

this works, but its a poor approach, as you are ripping out the item while you are iterating over it. it would be better to move the unset to the outer foreach, or else throw a break after the unset.
Thanks, didn't really think it through. fixed it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.