I’m : a web and mobile developer based in the Manning Valley, Australia.

Use external variable in array_filter

I needed to access an external variable filtering an array with PHP's array_filter function. I'd assumed that you could global it but that was always returning null. Turns out things have moved on and from 5.3 we've got access to the use keyword.

$foo = array_filter($bar, function($obj) use ($id) {
    if (isset($obj->foo)) {
        if ($obj->foo == $id) return true;
    return false;