urgent help with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

urgent help with arrays

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Re: urgent help with arrays

Post 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!
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: urgent help with arrays

Post 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;
Post Reply