RE: delete part of array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



On 13 January 2005 05:28, Matthew Fonda wrote:

> use the unset() function.
> 
> for ($i = 0; $i < count($array); $i++) {
> 	if (empty($array[$i]) {
> 		unset($array[$i]);
> 	}
> }

Without actually running it (I'm too lazy!), I'm pretty sure this won't work
correctly -- the count($array) is being recalculated each time round the
loop, so each time you do an unset() it will reduce by one, resulting in the
last few elements never being checked.

It should, however, be perfectly possible to use foreach in the same way:

  foreach ($array as $i=>$value) {
     if (empty($value]) {
        unset($array[$i]);
     }
  }

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux