Php filtering array problem

Hello,

I’m having some troubles with writing elegant code to filter out elements from php array.

Heres what I want to do:

I have 2 arrays:

$array = array('value 1', 'value 2', 'bleh', 'dont remove me');
$drop = array('value 1', 'bleh');

Now I want to filter // drop elements from $drop out of $array

I tried this (which seems to be the most elegant way to do it):

$filtered_array = array_filter($array, function($element) { return (!in_array($element, $drop)); } );

But I get:

Parse error: syntax error, unexpected T_FUNCTION in /Applications/MAMP/htdocs/ …

I even get the same error if I try to call this (I use PHP 5):

$test = array_filter($drop, function($a) { return !is_null($a) });

What is going on?

Thanks for help

What you are describing is already a defined function.

[FPHP]array_diff[/FPHP]

Thanks… but why doesn’t the code above work?

$filtered_array = array_filter($array, function($element) { return (!in_array($element, $drop)); } );
worked fine for me (other than throwing errors for $array and $element not being defined). No idea why yours is throwing a syntax error.

You need php 5.3 or higher to use anonymous functions