Passing variable to filter() callback

This is probably a general PHP question more than a Kirby question.

I’m doing a bunch of filtering via some AJAX calls. One of these filters is to check if any one of several parameters are found in a field. (example: each book has a region, I want to filter all books that are either north_america or europe or latin_america.

Here is a condensed version:

function contains($str, array $arr) {
  foreach($arr as $a) {
    if (stripos($str,$a) !== false) return true;
  return false;

foreach ($params as $param => $param_array) {
  switch($param) {
    case "regions":
      $books = $books->filter(function($book) { // this line!!
        if (contains((string)$book->collections(), $param_array)) {
          return $book;

how can I pass $param_array into the filter function?

$books = $books->filter(function($book, $param_array) { 

gives me an error:

Warning: Missing argument 2 for {closure}()

Is this a scope thing? or an array vs not-array thing?

I believe it works like this:

$books = $books->filter(function($book) use ($param_array) {

That did it!

I’m not sure what to google for, do you know what this is called? I’d like to read up on the PHP docs ~

Thank you!!

This is an anonymous function/closure. A starting point to get to know relevant vocabulary could be here: . #3 is about the use construct and how those values are passed along.