2017-07-26 112 views
-1

我在循环内部有一个Laravel ->each()函数。从'每个'函数的回调函数中访问变量

我希望能够从每个函数中获取循环变量。

foreach ($stringsArray as $string) { 
     if (!empty($string)) { 
      DBModel::all()->each(function (DBModel $model) { 
        global $string; 
        // $string at this point is nothing/undefined 

我怎样才能从->each()函数内访问$string

这是使用laravel 4.2。

与当前代码$string是不确定的(有或没有global指令)。

+1

的可能重复使用它:([PHP使用它以外的计算变量的回调函数] https://stackoverflow.com/questions/4588714/PHP-回调函数使用变量计算-外的-它) –

回答