在 where 子句 LINQ PHP 中发送变量

问题描述 投票:0回答:1

对于 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();
}

似乎不起作用,就像未定义

$find
一样,但我将其作为方法的参数发送。

php anonymous-function yalinqo phplinq
1个回答
3
投票

您可以使用

use
声明:

Enumerable::from($arr)
  ->where(function($val) use ($find) {
    return stripos($val->item, $find) > -1; 
  })
  ->toArray();
© www.soinside.com 2019 - 2024. All rights reserved.