2

Here is my code, throwing an error with Warning: Missing argument 2 for {closure}() in the first line

$all_together = array_filter($info,function($each_one,$extra){
    
    $op = $each_one["something"];
    
    if($op <= $extra) return $each_one["what_I_need"];
    
});

I need to use the $extra argument, independent of the input array elements. What am I missing exactly?

2 Answers 2

2

Seems like missing use keyword. Try this:

$all_together = array_filter($info, function($each_one) use ($extra) {
    $op = $each_one["something"];
    if($op <= $extra) return $each_one["what_I_need"];
});
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know, array filter should be used like this:

<?php
function test_odd($var)
{
return($var & 1);
}

$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>

Now only each element of the specified array will be passed to the function, if you must pass another argument, supply a default value to that argument in your function definition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.