7
\$\begingroup\$
$privilegeStrings = array_filter($privileges, function ($s) { return is_string($s); } );

Is there a better way to specify that I want just the string values in the given array?

\$\endgroup\$

1 Answer 1

11
\$\begingroup\$

You could do:

$privilegeStrings = array_filter($privileges, 'is_string');

array_filter passes every value of the array to the specified function. So you can just specify the function name and everything will be taken care of.

Reference

\$\endgroup\$
2
  • \$\begingroup\$ Ah.. didn't know you could pass the name of a function like that. Yuck! \$\endgroup\$ Commented Mar 3, 2011 at 20:58
  • 2
    \$\begingroup\$ @Billy ONeal - The name of a function is one acceptable form a callback may take. \$\endgroup\$ Commented Mar 4, 2011 at 3:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.