Fin the max value in array lower than x

just one approach from the other side:

function test(array $src, $limit) 
{
    $max = min($limit, max($src));
    $min = min($src);

    while (--$max >= $min) {
        if( in_array($max, $src) ) {
            return $max;
        }
    }
    return null;
}

O(n^2) is not the efficiency I’d consider best.

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.

agree. that’s my bad

[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.

array_reduce only uses a single loop.

True again, in your case it’s only one

Another way to do it is:

<?php

$haystack = array(1,5,2,3,7,1,2,4,8,4,0,9,11,15);

$max = (int) 8;

foreach ( $haystack AS $straw ) {
    if ( $straw < $max ) {
        $harvest[] = $straw;
    }
}

$needle = max($harvest);

echo "The needle is $needle";

?>

The loop culls the ones that are too high then it simply grabs the highest value of what’s left

array_filter would do that

you have one loop to extract the values less than the maximum and then effectively have a second loop with max to find the maximum of those left.

1 Like

I think you’re getting the code I posted mixed up with code posted by someone else in this thread. There is not second loop.

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.

1 Like

Hey @vivis93, hopefully you have enough choices here! :smiley:

1 Like

yeh to much choices cant decide which is best :yum:

1 Like

Mine is the best (array_reduce). Want a second opinion? Then mine is also the greatest.

2 Likes

Want a third opinion? :wink:

Try them all, and put them through their paces until you find which is really most efficient through multiple rounds of testing…

On hundreds of hardware, with varying CPU/RAM, hate to not see it thoroughly tested across the millions of possibilities :wink:

And, now you have a volunteer (@cpradio) to help, @vivis93 :smiley:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.