Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.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中的匿名函数_Php_Anonymous Types - Fatal编程技术网

php中的匿名函数

php中的匿名函数,php,anonymous-types,Php,Anonymous Types,想象一下(即使不是很好的样式),您有一个数组,其中添加了一些内容的数组。 也许是这样的: $result_array = array(); foreach( ... as $key => $value ) { $temp = array(); $temp["start"] = $value->start; $temp["end"] = $value->end; $result_array[] = $temp; } // Image it wi

想象一下(即使不是很好的样式),您有一个数组,其中添加了一些内容的数组。
也许是这样的:

$result_array = array();

foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $result_array[] = $temp;
}

// Image it will produce for example an $result_array like this:
   array(
       array("start" => 10, "end" => 20), 
       array("start" => 100, "end" => 120)
   );
foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $temp["duration"] = function() { return abs( __START__ - __END__ ); }
    $result_array[] = $temp;
}
$temp["start"] = $value->start;
$start = &$temp["start"];
$temp["end"] = $value->end;
$end = &$temp["end"];
$temp["duration"] = function() use (&$start, &$end) { return abs( $start - $end ); };
到目前为止还不错。现在让我们做一些肮脏的事情,比如: 我想添加一个匿名函数,返回每个临时数组中开始结束之间的范围。这可能看起来像这样:

$result_array = array();

foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $result_array[] = $temp;
}

// Image it will produce for example an $result_array like this:
   array(
       array("start" => 10, "end" => 20), 
       array("start" => 100, "end" => 120)
   );
foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $temp["duration"] = function() { return abs( __START__ - __END__ ); }
    $result_array[] = $temp;
}
$temp["start"] = $value->start;
$start = &$temp["start"];
$temp["end"] = $value->end;
$end = &$temp["end"];
$temp["duration"] = function() use (&$start, &$end) { return abs( $start - $end ); };
因为我在一个“数组”中,我不知道我的索引,我如何访问我的开始和结束时间

请不要因为糟糕的风格或语法错误而责骂我,这只是我在问题栏写下的一个例子


提前感谢。

匿名函数与其“上下文数组”没有任何连接。无法获取函数所在数组的值,因为函数和数组之间没有固有的连接。您最好在创建时通过闭包为其提供值:

$temp['duration'] = function () use ($value) { return abs($value->start - $value->end); }
当然,这在循环中不起作用,因为
$value
的值会发生变化,因此需要一个函数生成函数来中断该引用:

$temp['duration'] = call_user_func(function ($v) {
    return function () use ($v) { return abs($v->start - $v->end); };
}, $value);

我不知道你为什么要经历所有这些麻烦,而不是像平常一样计算并将值插入数组。

我真的不知道出于某种原因(我不知道)这是否是一种不好的做法,但你可以使用如下引用:

$result_array = array();

foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $result_array[] = $temp;
}

// Image it will produce for example an $result_array like this:
   array(
       array("start" => 10, "end" => 20), 
       array("start" => 100, "end" => 120)
   );
foreach( ... as $key => $value ) {
    $temp = array();
    $temp["start"] = $value->start;
    $temp["end"] = $value->end;
    $temp["duration"] = function() { return abs( __START__ - __END__ ); }
    $result_array[] = $temp;
}
$temp["start"] = $value->start;
$start = &$temp["start"];
$temp["end"] = $value->end;
$end = &$temp["end"];
$temp["duration"] = function() use (&$start, &$end) { return abs( $start - $end ); };

顺便问一下,你为什么要对这些值进行“惰性”操作?

对不起,我的意思是,为什么不能在循环时计算值,而不是使用匿名函数。循环之后这些值会改变吗?