0
 $count = 0;

$interpreter->addObserver(function(array $row) use (&$temperature) {
   $count+=1;

   if ($count < 3)  <----- not liking this 
   {

       return;

   }
    $temperature[] = array(
        'column1' => $row[16],
        'column2'  => $row[18],
    );
});

I am assuming it is a scope issue and I am unable to access the count from outside however I do need to count the rows in loop....thoughts?

DonCallisto
  • 28,203
  • 8
  • 66
  • 94
jini
  • 10,875
  • 34
  • 97
  • 167

1 Answers1

3

You could refer to the global by adding the following as the first line of your function:

global $count;

However, does it need to be global? You might create a static variable, which will retain its value between your method calls:

static $count = 0;
Andy G
  • 18,826
  • 5
  • 45
  • 66
  • 1
    Since he's using an anonymous function `use ($count)` would be much better than `global`. – deceze Jun 30 '13 at 09:38
  • @deceze I haven't used [anonymous functions](http://php.net/manual/en/functions.anonymous.php) with PHP, but shouldn't $count be passed by reference for this example? `use (&$count, &$temperature)` – Andy G Jun 30 '13 at 10:01
  • Yes, sure, it'd be a reference in this case. – deceze Jun 30 '13 at 10:15