Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 注意:未定义变量:计数行53 $count=0; $interpreter->addObserver(函数(数组$row)使用(&$temperature){ $count+=1; 如果($count_Php - Fatal编程技术网

Php 注意:未定义变量:计数行53 $count=0; $interpreter->addObserver(函数(数组$row)使用(&$temperature){ $count+=1; 如果($count

Php 注意:未定义变量:计数行53 $count=0; $interpreter->addObserver(函数(数组$row)使用(&$temperature){ $count+=1; 如果($count,php,Php,我假设这是一个范围问题,我无法从外部访问计数,但我确实需要对循环中的行进行计数……想法?您可以通过添加以下内容作为函数的第一行来引用全局值: $count = 0; $interpreter->addObserver(function(array $row) use (&$temperature) { $count+=1; if ($count < 3) <----- not liking this { return;

我假设这是一个范围问题,我无法从外部访问计数,但我确实需要对循环中的行进行计数……想法?

您可以通过添加以下内容作为函数的第一行来引用全局值:

 $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],
    );
});
然而,它是否需要全球化?您可以创建一个静态变量,该变量将在方法调用之间保留其值:

global $count;

您可以通过添加以下内容作为函数的第一行来引用全局函数:

 $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],
    );
});
然而,它是否需要全球化?您可以创建一个静态变量,该变量将在方法调用之间保留其值:

global $count;

由于他使用的是一个匿名函数
use($count)
global
@deceze要好得多,我还没有在PHP中使用过,但是对于这个例子,$count不应该通过引用传递吗
use(&$count,&$temperature)
是的,当然,在这种情况下它是一个引用。因为他使用的是一个匿名函数
use($count)
比我没有在PHP中使用的
global
@deceze要好得多,但是对于这个例子,$count不应该通过引用传递吗
使用(&$count,&$temperature)
是的,当然,在这种情况下它是一个参考。我很惊讶你对
$temperature
的使用权是正确的,但不是
$count
。我很惊讶你对
$temperature
的使用权是正确的。