• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7808
  • Last Modified:

clearing empty elements from an array

Hi,
How do you clear an array of empty elements?
0
mcm-mgm
Asked:
mcm-mgm
  • 4
  • 3
  • 2
  • +3
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
RoonaanCommented:
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now