I'm curious if there's a better way to do what I'm doing. I'm fairly new to php so I'm interested in what others think who have spent more time with the language.
What I want to do: call a function on every object in an array
What I'm doing:
array_map(function($object) { $object->loadEvents(); }, $patients);
This is fine, and it works. I could also use a for loop,
Why I'm asking: I've become accustomed to not using for loops when I don't have too, so I figured out a way to use array_map. The thing is, every where I look, it seems people are using array_map to map results to a new array. When I basically want the functionality of array_map but without the return values.
Is there a better way? Outside this and a for loop? Is a for loop a better way?
array_map()will also use an internal loop to go through the array values. Also you might want to look atarray_walk()foreachis the usual way.array_walk()is the version ofarray_map()that doesn't build a new array.