Skip to content

Howto - php array_filter anonymous function

Every day something new :-). This day i finally had time to deal with anonymous functions. Since it is the first time, also after solving the problems, working with anonymous functions is feeling a bit strange. But now i have done it, so it feels better then before where i have never dealt with them.

Here is my example code.

<?php $nl = '<br>' . PHP_EOL; $array = array( 0 => array( 'level' => 0, 'label' => 'zero', ), 1 => array( 'level' => 1, 'label' => 'one' ), 2 => array( 'level' => 0, 'label' => 'two', ), 3 => array( 'level' => 2, 'label' => 'three', ), ); $levelToFilter = 1; $array2 = array_filter($array, function ($arrayItem) use ($levelToFilter) { return ($arrayItem['level'] !== $levelToFilter); }); echo 'array::' . $nl; echo xdebug_var_dump($array); echo 'array2::' . $nl; echo xdebug_var_dump($array2);
Feel free to play around with it. I guess $levelToFilter is the variable you should change :-).


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Markdown format allowed
Form options