4

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?

Kristofer Doman
  • 439
  • 1
  • 7
  • 12
  • Why are you so afraid of for loops? At the end `array_map()` will also use an internal loop to go through the array values. Also you might want to look at `array_walk()` – Rizier123 Aug 10 '16 at 17:06
  • `foreach` is the usual way. – Barmar Aug 10 '16 at 17:06
  • As @Rizier123 suggests, `array_walk()` is the version of `array_map()` that doesn't build a new array. – Barmar Aug 10 '16 at 17:07
  • If you try to use something just because it seems fashionable, you're going to have a bad time. Just use the best tool for the job. If a loop is the simplest and cleanest solution, use that. – BadHorsie Aug 10 '16 at 17:08

2 Answers2

4

The usual idiom in PHP for looping over an array is the foreach operator. array_walk and array_map can be used, but they didn't become common idioms because until relatively recent versions of PHP creating anonymous functions was inconvenient. Prior to PHP 5.3, you had to call create_function() to create a function on the fly. So array_walk and array_map were usually only used when there's a named function that does what you want, e.g.

$array = array_map('trim', $array);

But even now that you can use function to create anonymous functions, many people find

array_walk($array, function($x) {
    ...
});

less readable than

foreach ($array as $xx) {
    ...
}

array_map tends to be used more than array_walk because it can be used as part of a larger expression. With foreach you would have to use one loop to push onto a result array, then follow it with code to operate on that array.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This actually didn't work for me because I'm changing the instances with the function loadEvents. Apparently there's a problem with references and array_walk, so many a for loop is just easiest in this case. I appreciate the help though. – Kristofer Doman Aug 10 '16 at 20:19
  • I don't think there should be any issue with objects and `array_walk`. I had the arguments to `array_walk` backwards in my answer, maybe that was the problem? – Barmar Aug 10 '16 at 20:25
  • Oh yeah, that was the problem! Thanks a lot, works perfectly! – Kristofer Doman Aug 10 '16 at 20:38
0

What is wrong with using a loop? That's the exact purpose it's designed for.

foreach ($patients as $patient) {
    $patient->loadEvents();
}

Simple and clear to everyone. What is the point in writing verbose code to achieve the same job, just because it seems "cool" or what everyone is doing?

I've become accustomed to not using for loops when I don't have to, so I figured out a way to use array_map.

You are using array_map() incorrectly for the sake of an abitrary rule you've set yourself that you won't use a loop?

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • 1
    This seems like more of just a comment. I agree with it but you basically ignored the question and left out the obvious answer of `array_walk`. – nerdlyist Aug 10 '16 at 18:38
  • I'm not using anything. No one said once there was a problem with a for loop, there isn't. The question was asked to gain knowledge. – Kristofer Doman Aug 10 '16 at 20:08
  • @nerdlyist Well I haven't ignored the question at all. It literally starts with *"I'm curious if there's a better way to do what I'm doing."* and my answer is yes, use a `foreach` loop rather than `array_map` which is not fit for purpose, and `array_walk` which is basically a grander way of doing the same thing as a straightforward `foreach` loop in this case. The `foreach` is clearly more readable and dies the exact job required, and programming is about finding optimal, efficient and tidy solutions to problems. – BadHorsie Aug 11 '16 at 10:19
  • @KristoferDoman You literally asked in your question *"Is a for loop a better way?"* and also said *"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"* which makes no sense from a programming perspective if you are trying to write good code. – BadHorsie Aug 11 '16 at 10:22
  • @BadHorsie I personally find foreach less readable, so there's nothing clear there. You're just arguing to argue, I'm done. – Kristofer Doman Aug 11 '16 at 16:20
  • @KristoferDoman I was just explaining my answer, I didn't realise you would be so offended. It's your code - whatever makes you happy. – BadHorsie Aug 11 '16 at 17:36