It is interesting that while trying (for some reason) to avoid an explicit loop with constant complexity O(n), PHP users ends up with n^2 nested loops disguised as built-in PHP functions - min(), in_array() or sorting of all sorts.
Regardless of how easy it is to read, any code that tries to compare different types is a no no in my book. Life is too short to worry about things like is null less than -1 or not. Bad bad bad.
array_reduce only uses a single loop. Oddly enough it is a bit slower then foreach but comparable.
Back when php closures were first introduced, I remember smiling and thinking it was just an attempt to support another buzzword. But as I started to see code using closures and started to write a few myself I came to realize that they really are quite powerful and easy enough to read and maintain. Subjective of course but closures are now a major part of my toolkit.
And closures make things like array_map, array_filter, array_reduce, usort easy to use and understand. In my opinion of course.
[quote=“ahundiak, post:23, topic:235256”]
And closures make things like array_map, array_filter, array_reduce, usort easy to use and understand. In my opinion of course.[/quote]
True. In case where such a function you really need.
max() is the second loop.How else would it determine the max value without looping through the values passed to it - not a loop in the code you posted but still calling a loop to perform its processing.