1

I have the following array:

Array
(
    [active_x] => 0
    [active_y] => 0
    [regions] => Array
        (
            [0] => Array
                (
                    [x] => 0
                    [y] => 0
                    [name] => The Campground
                    [monsterProbability] => 5
                    [monsterDifficulty] => 1
                    [monsterType] => earth
                )

        )

)

This array can contain multiple arrays inside of the regions key.

I'm creating a function that will give me the data for a specific region inside the regions key, based on active_x and active_y.

I've tried using array_filter(), but I get no return.

The array above is called $map:

$x = $map['active_x'];
$y = $map['active_y'];

$data = array_filter($map['regions'], function ($var) {
    return ($var['x'] == $x && $var['y'] == $y);
});

echo "<pre>".print_r($data, true)."</pre>";

How can I retrieve only the array containing the information for the specific active region?

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71

1 Answers1

2

It looks like it should work, as long as you get $x and $y into the scope of your callback.

$data = array_filter($map['regions'], function ($var) use ($x, $y) {
    return ($var['x'] == $x && $var['y'] == $y);
});
Don't Panic
  • 41,125
  • 10
  • 61
  • 80