0

at one point in my function I need to filter an array based on a last digit of a key, but I cannot pass argument to the function I'm using to filter

here's the code:

    function functionName($number){
        //some code
        $items = array_filter(SomeClass::$items, function ($item, $number){
            return ($item%10 == $number);
        }, ARRAY_FILTER_USE_KEY);
        //some code
    }

what can I do to make sure the inner function uses $number passed to functionName()?

nha
  • 17,623
  • 13
  • 87
  • 133
Zbyszek Kisły
  • 2,110
  • 4
  • 26
  • 48

1 Answers1

0

You can pass variables to closure function by "use" Eg.

function ($var) use ($yourVarThatYouWannaPass) { return $var;}
Eakethet
  • 682
  • 1
  • 5
  • 18