clearing empty elements from an array

Hi,
How do you clear an array of empty elements?
mcm-mgmAsked:
Who is Participating?
 
RoonaanConnect With a Mentor Commented:
Just refreshing my array functions. It seems that array_filter does remove all items that resolve to "false" when you do not provide a proper callback function:

http://php.net/array_filter

<?php

$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => ''
         );

print_r(array_filter($entry));
?>  

The above example will output:

Array
(
    [0] => foo
    [2] => -1
)

Kind regards

-r-
 
0
 
Diablo84Commented:
What do you mean by clear?

You can unset the array using:

unset($arrayname);

or you can effectively reset it using:

$arrayname = array();

Diablo84
0
 
Diablo84Commented:
sorry, re read the question...

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

Diablo84
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ldbkuttyCommented:
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.
0
 
RoonaanCommented:
The loop as mentioned by ldbkutty:

<?php

reset($array);

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

-r-
0
 
Diablo84Commented:
$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
)
0
 
jdpipeCommented:
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
0
 
mensuckCommented:
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
0
 
jdpipeCommented:
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
0
 
mcm-mgmAuthor Commented:
Is there a way to compress the key as well?
0
 
Diablo84Commented:
As i recall  you can use array_merge to reindex the keys when numeric indexes are used, eg:

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

-r-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.