0

I wrote this

 $result = array();

        array_map(function($row) use ($result) {
            $result[$row->id] = array();
            $result[$row->id]['geojson'] = $row->geojson;
        }, $regions);

and $result is empty at the end.

Is it possible to populate array this way?

1
  • 1
    Looks like it should work, what is var_dump($regions);? Commented Apr 6, 2017 at 20:05

1 Answer 1

6

$result inside the function is a copy of the outer array, so changes you make don't affect the original. You need to use a reference: use (&$result)

array_map(function($row) use (&$result) {
    $result[$row->id] = array();
    $result[$row->id]['geojson'] = $row->geojson;
}, $regions);

Or you could simply use foreach

foreach ($regions as $row) {
    $result[$row->id] = array();
    $result[$row->id]['geojson'] = $row->geojson;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.