-1

What is the default value for callback argument of array_filter function?

Let imagine we have array:

$input = [
    "Required field" => true
    "Please, enter a number" => false
];

Normally, I use array_filter without explicitly setting callback parameter, as array_filter( $input) and this function will return the following array:

$input = [
    "Required field" => true
];

But now I need to exactly set callback argument. So when I'm sending null as callback this functions return null and "non empty" value ("Required field" => true) is ignored, what would I not like.

Of course, I can call the function with own callback function like:

array_filter( $input, fn($item) => !empty($item) )

But I was hoping for more elegant way

9
  • Could you provide the input and desired output that you need for this case where simply calling array_filter without a callback is not sufficient for you? Commented Nov 22, 2020 at 13:32
  • 1
    Concerning your opening question, it is described in the docs you have linked yourself: If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case. Commented Nov 22, 2020 at 13:34
  • I just want to filter array from empty values with explicitly setting callback argument, but in more elegant way rather than setting own callback function. Commented Nov 22, 2020 at 13:47
  • The most elegant way really is not setting a callback. Why would you pass a callback that does exactly the same thing as the default? Commented Nov 22, 2020 at 13:50
  • I want to make array_filter_recursive function. Like in this example: stackoverflow.com/a/6795671/7265862. As you see, in the second function, there is $callback argument, which later will be passed to array_filter function. I don't want to make if else statement to check for null $callback argument. That's why I was asking community for elegant solution Commented Nov 22, 2020 at 14:03

1 Answer 1

1

If the source code of array_filter contains a second parameter, it is checked whether it is a real callable function. A string can also be passed. But even then it is checked whether this is a real function.

Variant 1: Define a function, then use its name

function isNotEmpty($val){
  return !empty($val);
}

$arr = [0,1,[],null,false,"a","",(object)[],-1];

$res = array_filter( $arr);
$res1 = array_filter( $arr, 'isNotEmpty');
    
var_dump($res === $res1); //bool(true)

Variant 2: Use the boolval function

$arr = [0,1,[],null,false,"a","",(object)[],-1];

$res = array_filter( $arr);
$res1 = array_filter( $arr, 'boolval');

var_dump($res === $res1);  //bool(true)

With the function boolvar () this also works apparently. However, the PHP manual says:

var The scalar value being converted to a bool.

But the examples for the boolval function also show an empty array as a value.

Sign up to request clarification or add additional context in comments.

2 Comments

Variant 2 is what I was searching for. Thank you!
Is there any link to the article about remark? I can't find it out

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.