简体   繁体   English

如何在PHP中找到值大于X的第一个数组元素?

[英]How to find first array element with value greater than X in PHP?

I have array with numeric values and I want to get key of first element which has value equal or greater than 5. Is there more elegant way than looping all elements in foreach ?我有一个数值数组,我想获得第一个元素的键,它的值等于或大于 5。有没有比在foreach循环所有元素更优雅的方法?

// "dirty" way
foreach ([0, 0, 4, 4, 5, 7] as $key => $value) {
    if ($value >= 5) {
        echo $key;
        break;
    }
}

The algorithm itself is perfectly fine, don't touch it.算法本身非常好,不要碰它。

That said, you could add some ribbons by writing a generic search function:也就是说,您可以通过编写通用搜索功能来添加一些功能区:

// find first key (from beginning of $a) for which the corresponding
// array element satisfies predicate $fn
function array_find(array $a, callable $fn)
{
    foreach ($a as $key => $value) {
        if ($fn($value, $key, $a)) {
            return $key;
        }
    }
    return false;
}

$key = array_find([0, 0, 4, 4, 5, 7], function($value) {
    return $value >= 5;
});

Now, although this is a more elegant approach, it's less efficient;现在,虽然这是一种更优雅的方法,但效率较低; there's a considerable overhead of calling the closure at each item.在每个项目上调用闭包都有相当大的开销。 If performance is paramount, use what you have and run with it.如果性能是最重要的,请使用您拥有的并运行它。

I have array with numeric values and I want to get key of first element which has value equal or greater than 5. Is there more elegant way than looping all elements in foreach ?我有一个带有数值的数组,我想获取值等于或大于5的第一个元素的键。有没有比在foreach循环所有元素更好的方法了?

// "dirty" way
foreach ([0, 0, 4, 4, 5, 7] as $key => $value) {
    if ($value >= 5) {
        echo $key;
        break;
    }
}

Using array_search() can be efficient when seeking the earliest occurring match, but it is inappropriate in this case because it will not allow you to supply your required logic to the search.在寻找最早出现的匹配时,使用array_search()可能很有效,但在这种情况下不合适,因为它不允许您为搜索提供所需的逻辑。

Using functional iterators like array_map() and array_filter() are not ideal because they lack the ability to "short circuit" as soon as a match is made.使用像array_map()array_filter()这样的函数式迭代器并不理想,因为它们缺乏在匹配后立即“短路”的能力。 In my own professional projects, I would not use a functional-style technique -- even on a relatively small data set -- because there is no valuable gain in doing so.在我自己的专业项目中,我不会使用函数式技术——即使是在相对较小的数据集上——因为这样做没有任何有价值的收益。

Considering the above, just use a classic loop with a condition in its body which breaks the loop as soon as a qualifying match occurs.考虑到上述情况,只需使用在其主体中带有条件的经典循环,该条件在合格匹配发生时立即中断循环。

The $test variable is defined within a loop of test cases in my runnable demo links. $test变量是在我的可运行演示链接中的测试用例循环中定义的。 To trial my code below, be sure to define $test as your needle before entering the loop.要在下面试用我的代码,请确保在进入循环之前将$test定义为您的指针。

Earliest key where array value is greater than or equal to the needle : ( Demo )数组值大于或等于针的最早键:(演示

$array = [0, 0, 4, 4, 5, 7];

$foundKey = 'not found';
foreach ($array as $key => $value) {
    if ($value >= $test) {
        $foundKey = $key;
        break;
    }
}

Get key of highest value not greater than the needle : ( Demo )获取不大于针的最高值键:(演示

$array = [0, 0, 4, 4, 5, 7];

$foundKey = 'not found';
foreach ($array as $key => $value) {
    if ($value > $test) {
        break;
    }
    $foundKey = $key;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM