You are here: RE: [PHP] delete part of array « PHP « IT news, forums, messages
RE: [PHP] delete part of array

Posted by Mike Ford on 01/13/05 19:54

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@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация