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?

4
  • 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() Commented Aug 10, 2016 at 17:06
  • foreach is the usual way. Commented Aug 10, 2016 at 17:06
  • As @Rizier123 suggests, array_walk() is the version of array_map() that doesn't build a new array. Commented Aug 10, 2016 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. Commented Aug 10, 2016 at 17:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
Oh yeah, that was the problem! Thanks a lot, works perfectly!
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?

6 Comments

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.
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.
@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.
@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 I personally find foreach less readable, so there's nothing clear there. You're just arguing to argue, I'm done.
|

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.