phpanonymous-functionyalinqophplinq

Send a variable in where clause LINQ PHP


For LINQ in PHP, I used https://github.com/Athari/YaLinqo

I don't know how to pass a variable in where clause.

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

Seems is not working like $find is not defined but I send it as method's parameter.


Solution

  • You could use use statement:

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