搜索数组:array_filter 与循环
我是 PHP 新手,需要有关数组搜索的建议。
如果我想搜索多维数组内的元素,我可以使用 array_filter ,也可以循环遍历数组并查看是否存在与我的条件匹配的元素。
我在很多地方都看到这两个建议。哪个更快?下面是一个示例数组。
Array (
[0] => Array (
[id] => 4e288306a74848.46724799
[question] => Which city is capital of New York?
[answers] => Array (
[0] => Array (
[id] => 4e288b637072c6.27436568
[answer] => New York
[question_id_fk] => 4e288306a74848.46724799
[correct] => 0
)
[1] => Array (
[id] => 4e288b63709a24.35955656
[answer] => Albany
[question_id_fk] => 4e288306a74848.46724799
[correct] => 1
)
)
)
)
我正在这样寻找。
$thisQuestion = array_filter($pollQuestions, function($q) {
return questionId == $q["id"];
});
I am really new in PHP and need a suggestion about array search.
If I want to search for an element inside a multidimensional array, I can either use array_filter
or I can loop through the array and see if an element matching my criteria is present.
I see both suggestion at many places. Which is faster? Below is a sample array.
Array (
[0] => Array (
[id] => 4e288306a74848.46724799
[question] => Which city is capital of New York?
[answers] => Array (
[0] => Array (
[id] => 4e288b637072c6.27436568
[answer] => New York
[question_id_fk] => 4e288306a74848.46724799
[correct] => 0
)
[1] => Array (
[id] => 4e288b63709a24.35955656
[answer] => Albany
[question_id_fk] => 4e288306a74848.46724799
[correct] => 1
)
)
)
)
I am searching like this.
$thisQuestion = array_filter($pollQuestions, function($q) {
return questionId == $q["id"];
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道,这个问题很老了,但我不同意公认的答案。我还想知道
foreach()
循环和array_filter()
函数之间是否存在差异,并发现以下帖子:http://www.levijackson.net/are-array_-functions-faster-than-loops/< /a>
(存档:https://web.archive.org/web/20200930002558/https://www.levijackson.net/are-array_-functions-faster-than-loops/)
李维杰克逊做得很好并比较了几个循环和 array_*() 函数的速度。根据他的说法,
foreach()
循环比array_filter()
函数更快。虽然它通常不会产生这么大的差异,但当您必须处理大量数据时,它就开始变得重要了。I know, the question is old, but I disagree with the accepted answer. I was also wondering, if there was a difference between a
foreach()
loop and thearray_filter()
function and found the following post:http://www.levijackson.net/are-array_-functions-faster-than-loops/
(archive: https://web.archive.org/web/20200930002558/https://www.levijackson.net/are-array_-functions-faster-than-loops/)
Levi Jackson did a nice job and compared the speed of several loop and
array_*()
functions. According to him aforeach()
loop is faster than thearray_filter()
function. Although it mostly doesn't make such a big difference, it starts to matter, when you have to process a lot of data.我制作了一个测试脚本,因为我有点怀疑......内部函数怎么会比循环慢......
但实际上这是真的。另一个有趣的结果是 php 7.4 几乎比 7.2 快 10 倍!
你可以自己尝试一下
I've made a test script because I was a little skeptical ...how can an internal function be slower than a loop...
But actually it's true. Another interesting result is that php 7.4 is almost 10x faster than 7.2!
You can try yourself
我知道这是一个老问题,但我要说两点:对我来说,使用 foreach 循环比使用 array_filter 快得多。使用foreach,按id执行搜索需要1.4秒,使用过滤器需要8.6秒。
I know it's an old question, but I'll give my two cents: for me, using a foreach loop was much faster than using array_filter. Using foreach, it took 1.4 seconds to perform a search by id, and using the filter it took 8.6 seconds.
根据我自己的经验,
foreach
更快。我认为这与函数调用开销、参数检查、复制到变量返回指令等有关。当使用基本语法时,解析的代码更有可能更接近编译/解释的字节码,有更好的优化核心。共同的规则是:任何事情都更简单,运行更快(意味着更少的检查,更少的功能,只要它有你需要的一切)
From my own experience,
foreach
is faster. I think it has something to do with function call overhead, arguments check, copy to variable return instruction, etc.. When using a basic syntax, the parsed code is more likely to be closer to the compiled/interpreted bytecodes, have better optimization down the core.The common rule is : anything is simplier, run faster (imply less check, less functionnality, as long as it has all you need)
Array_Filter
至于我也一样。
Array_Filter
as for me same.