在where子句中发送变量LINQ PHP

Sna*_*yes 5 php linq anonymous-function yalinqo

对于 PHP 中的 LINQ,我使用了https://github.com/Athari/YaLinqo

我不知道如何在where子句中传递变量。

public function filter($arr, $find) {
   Enumerable::from($arr)->where(function($val) { return stripos($val->item, $find) > -1; })->toArray();
}
Run Code Online (Sandbox Code Playgroud)

似乎没有像$find未定义那样工作,但我将它作为方法的参数发送。

luc*_*cky 3

您可以使用use声明:

Enumerable::from($arr)
  ->where(function($val) use ($find) {
    return stripos($val->item, $find) > -1; 
  })
  ->toArray();
Run Code Online (Sandbox Code Playgroud)