Link to home
Start Free TrialLog in
Avatar of mcm-mgm
mcm-mgm

asked on

clearing empty elements from an array

Hi,
How do you clear an array of empty elements?
Avatar of Diablo84
Diablo84

What do you mean by clear?

You can unset the array using:

unset($arrayname);

or you can effectively reset it using:

$arrayname = array();

Diablo84
sorry, re read the question...

foreach ($arrayname as $key => $value) if (empty($arrayname[$key])) unset($arrayname[$key]);

Diablo84
A note: foreach creates a copy of the original array and loops through that copy. In order to directly deal with the original array, its better to use for loop.
Avatar of Roonaan
The loop as mentioned by ldbkutty:

<?php

reset($array);

while(list($key, $value) = each($array))
  if(empty($value))
    unset($array[$key]);
?>

-r-
$array = array('one','','three','four','','six');
foreach ($array as $key => $value) if (empty($array[$key])) unset($array[$key]);
print_r($array);

output:

Array
(
    [0] => one
    [2] => three
    [3] => four
    [5] => six
)

$array = array('one','','three','four','','six');
reset($array);
while(list($key, $value) = each($array))
  if(empty($value))
    unset($array[$key]);
print_r($array);

output:

Array
(
    [0] => one
    [2] => three
    [3] => four
    [5] => six
)
Just to make the tiniest, tiniest improvement to diablo's code there,

...
while(list($key,) = each($array))
  if(empty($array[$value]))
      ...

This saves copying $value from the array into new memory, which is good if you are dealing with large data items in the array.

JP
If the keys are of the type numeric! Then you can use my little trick, instead of doing a loop! This works great for small or big arrays! I use something like this when removing empty elements from a mailbox UID array that can sometimes have over 10000 elements. It's much faster than looping in my case! I use '|' as my character to keep the elements a part, but you can use any character or group of characters so you make it unique, IE: something that would not appear in any of the array values!

As a side note this was discussed among the PHP developers the other day in great detail. I think you will see a core array function that will remove empty elements sometime soon!

<?

// a unorthodox way that works

$old_array = array ( 7, '', 4, 9, '', 11, '', 13, 77, '', 'It was a sunny day', '', '', '' );

$old_array = explode ( '|', preg_replace ( "!(^[\|]+|[\|]+$)!", "", preg_replace ( "![\|]+!", "|", implode ( '|', $old_array ) ) ) );

print_r ( $old_array );

?>


Suzanne
hi mensuck

another trick would be using array_flip, perhaps.

$x=@array_flip($a);
unset($x['']);
$a = array_flip($x);
print_r($a);

Note that you need to suppress the warning from array_flip about only being able to flip string/integer values.

JP
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcm-mgm

ASKER

Is there a way to compress the key as well?
As i recall  you can use array_merge to reindex the keys when numeric indexes are used, eg:

$array= array_merge($array);
instead of array_keys, you could obviously just use:
$array = array_values($array);

-r-