Can I pass an additional variable into map()?

I am looping over a structure field and need to use the map() function on each structure item. However, within this map function I would need to use a top level variable.

I tried to pass it into the function like this:

[...]->map(function($item, &$myVar) {
  // mapping stuff

But it gives me an error:

Too few arguments to function Kirby\Cms\App::{closure}(), 1 passed and exactly 2 expected

Any idea how I could use $myVar inside the map function?


Yes, with the use() statement:

[...]->map(function($item) use($myVar) {
  // mapping stuff

Thank you, didn’t know about the use() operator. Works great!