Page 1 of 1

urgent help with arrays

Posted: Sat May 01, 2010 8:15 am
by gammaman
How can i take the array below and remove the empty values and then reorder the array?

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


So this would then become [0]=> 20 , [1] => 40


Thank you in advance.

Re: urgent help with arrays

Posted: Sat May 01, 2010 8:36 am
by DaveTheAve

Code: Select all

$tmp = array();
foreach($array as $key=>$value) {
  if (!empty($key)) {
     $tmp[] = $value;
  }
}
$array = $tmp;
unset($tmp);
Not tested but should work without errors! Enjoy!

Re: urgent help with arrays

Posted: Sat May 01, 2010 8:44 am
by gammaman
Yes this work thanks. One thing though

$tmp = array();
foreach($array as $key=>$value) {
if (!empty($key)) {
$tmp[] = $value;
}


it should be !empty $value;