views:

118

answers:

2

I have an array of arrays, each array containing details of a scan by a medical device. I'm getting this data from text logs that are dumped nightly. The format of which is this:

$this->scans = array(
  array(  
    'patientid' => (int),
    'patientname' => 'John Skeet',
    'reviewed' => 0 or 1
     //plus more irrelevant
  ),
  array(
    //same as above
  ), //etc
)

The important array key here is reviewed, as each scan may be reviewed if it is of high enough quality. However, the text logs dump out EVERY scan that is acquired, then goes back through and re-lists the ones that are reviewed.

Now in order to prevent duplicates , I figured I could just use an array_filter to filter out scans that have been both acquired and reviewed (keeping the reviewed version). However, the filter function is filtering out the entire array (except in some rare cases). If someone could take a look and let me know why they think it's happening that would be much appreciated.

$this->scans = array_filter($this->scans, array($this, "scan_cleanup"));

.

private function scan_cleanup($scan) {
        //only if the scan was not reviewed
 if ($scan['reviewed'] == 0) {
                //change reviewed status to see if there is a duplicate
  $scan['reviewed'] == 1;
                //return false to remove this copy (and keep reviewed)
  if (in_array($scan, $this->scans)) {
   return false;
  }
 }
 return true;

}
+2  A: 
$scan['reviewed'] == 1;

vs

$scan['reviewed'] = 1;

One is a conditional, that does nothing in this context, the other is not there.

You are also not running the return false very often. I'd change the logic a little to make it a little clearer, and simpler by a little refactoring (pulling out a condition-check).

if ($scan['reviewed'] and hasDupe($scan)) {
   return false;  // filter out
}
return true; // it is passed back, and is output

hasDupe() does the best checks you know for a duplicate record and returns true/false.

Alister Bulman
Figures, it's always something so simple. I spent forever debugging this. Thanks.
tj111
the update I added will also help a lot in terms of readability
Alister Bulman
+1  A: 

Simple case of "==" vs. "=" as far as I can see.

$scan['reviewed'] = 1;

That oughta do the trick. Sometimes the simplest problems are the hardest to spot ;-)

Gabriel Hurley