By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,705 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,705 IT Pros & Developers. It's quick & easy.

Evaluating array and skipping certain results

P: n/a
I'm having a brain freeze on this. I have a script where I am reading
the contents of a file into an array. I have that up and working with no
problem. Then I'm exploding the array using

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));
}

Works fine. But what I want to do now is evaluate arrayData[2] to see if
it contains the integers "00" (that's zero zero) and if it does, I want
it to skip that entry. I know I need to set it up with
if arrayData[2] == "00" {

But I'm drawing a blank on how to get it to skip this entire line in the
final results. There may be multiple instances of this happening in each
file and I want to skip them all. Can anyone point me in the right
direction for the coding to accomplish this? Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
NC
JackM wrote:

I have a script where I am reading the contents of a file into
an array. I have that up and working with no problem. Then I'm
exploding the array using

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));
}

Works fine. But what I want to do now is evaluate arrayData[2]
to see if it contains the integers "00" (that's zero zero) and
if it does, I want it to skip that entry.


That's where continue operator comes in handy:

while ($line = fgets($fd, 4096)) {
$arrayData = explode("-", trim($line));
if (arrayData[2] == '00') {
continue;
}
// do stuff
}

Alternatively, you can simply do:

while ($line = fgets($fd, 4096)) {
$arrayData = explode("-", trim($line));
if (arrayData[2] <> '00') {
// do stuff
}
}

Cheers,
NC

Jul 17 '05 #2

P: n/a
NC <nc@iname.com> wrote:
if (arrayData[2] == '00') {
if (arrayData[2] <> '00') {


A recipie for disaster. If $arrayData[2] equals '0' this evalutates to
true and false resp. If OP really wants to check for '00' he should use
strcmp, === (or !==).

Jul 17 '05 #3

P: n/a
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/
Jul 17 '05 #4

P: n/a
Ewoud Dronkert wrote:
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.
}
}


Thanks to all who offered solutions. You people are the best. It seems
that Ewoud's suggestion is the easiest to implement and, better still,
it works for my purposes. ;) I'll have to read up on the continue
control structure to see if it would be suitable for other things I do.
Much obliged to everyone.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.