You are here: Re: Evaluating array and skipping certain results « PHP Programming Language « IT news, forums, messages
Re: Evaluating array and skipping certain results

Posted by Ewoud Dronkert on 05/21/05 12:02

On Fri, 20 May 2005 22:21:34 -0400, JackM wrote:
> while ($line = fgets($fd, 4096)) {
> $arrayData = explode("-", trim($line));
> }

What might be conceptually simpler is

function nonzero($line) {
$a = explode('-', trim($line));
return strcmp('00', $a[2]);
}
$rawdata = file('myfile.ext'); //read into array by lines
$filtered = array_filter($rawdata, 'nonzero');

but it may lead to doing things twice if you need the exploded line
again later. If you only explode() to check for '00', this is probably
fine. If you do need to process each line in its exploded form, you
might want to explode first, then filter:

function expline($line) {
return explode('-', trim($line));
}
function nonzero($arr) {
return strcmp('00', $arr[2]);
}
$rawdata = file('myfile.ext');
$expdata = array_map('expline', $rawdata);
$filtered = array_filter($expdata, 'nonzero');

Then process the elements of $filtered (each element is now an array),
maybe with array_map() again. Nice if you like functional programming.
Shortest imperative version:

$rawdata = file('myfile.ext');
foreach ($rawdata as $line) {
$arr = explode('-', trim($line));
if (strcmp('00', $arr[2])) { //skip if arr[2] is 00
//do stuff with $arr[0], $arr[1], etc.
}
}


--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/

 

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

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