83

I've got an array, which I want to filter by an external variable. The situation is as follows:

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

The first var_dump returns the ID (which is dynamically set ofcourse), however, the second var_dump returns NULL.

Can anyone tell me why, and how to solve it?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Sander Koedood
  • 6,007
  • 6
  • 25
  • 34

3 Answers3

175

The variable $id isn't in the scope of the function. You need to use the use clause to make external variables accessible:

$foo = array_filter($bar, function($obj) use ($id) {
    if (isset($obj->foo)) {
        var_dump($id);
        if ($obj->foo == $id) return true;
    }
    return false;
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
13

Variable scope issue!

Simple fix would be :

$id = '1';
var_dump($id);
$foo = array_filter($bar, function($obj){
    global $id;
    if (isset($obj->foo)) {
        var_dump($id);
        if ($obj->foo == $id) return true;
    }
    return false;
}); 

or, since PHP 5.3

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

Hope it helps

php-dev
  • 6,998
  • 4
  • 24
  • 38
  • 1
    I don't think the first version would work pre-5.3, since anonymous functions were added in 5.3. Earlier, you'd have to use the `create_function()` function, along with the `global` declaration. – Barmar Feb 06 '17 at 22:11
9

Because your closure function can't see $id. You need the use keyword:

$foo = array_filter($bar, function($obj) use ($id) {
Joe
  • 15,669
  • 4
  • 48
  • 83