php 搜索数组:array_filter vs loop

2eafrhcq  于 2023-05-16  发布在  PHP
关注(0)|答案(5)|浏览(85)

我真的是新的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"];
});
jum4pzuy

jum4pzuy1#

我知道,这个问题很老了,但我不同意公认的答案。我也想知道,foreach()循环和array_filter()函数之间是否有区别,并发现了以下帖子:
http://www.levijackson.net/are-array_-functions-faster-than-loops/
LeviJackson做得很好,比较了几个循环和array_*()函数的速度。根据他的说法,foreach()循环比array_filter()函数快。虽然它通常不会产生如此大的差异,但当您必须处理大量数据时,它开始起作用。

djp7away

djp7away2#

我做了一个测试脚本,因为我有点怀疑……一个内部函数怎么会比一个循环慢……
但事实上这是真的。另一个有趣的结果是php7.4比7.2快了将近10倍!
你可以自己试试

<?php
/*** Results on my machine ***
php 7.2
array_filter: 2.5147440433502
foreach: 0.13733291625977
for i: 0.24090600013733

php 7.4
array_filter: 0.057109117507935
foreach: 0.021071910858154
for i: 0.027867078781128

php 8.2
array_filter: 0.069692134857178
foreach: 0.018320083618164
for i: 0.019519329071045
**/

ini_set('memory_limit', '500M');
$data = range(0, 1000000);

// ARRAY FILTER
$start = microtime(true);
$newData = array_filter($data, function ($item) {
    return $item % 2;
});
$end = microtime(true);

echo "array_filter: ";
echo $end - $start . PHP_EOL;

// FOREACH
$start = microtime(true);
$newData = array();
foreach ($data as $item) {
    if ($item % 2) {
        $newData[] = $item;
    }
}
$end = microtime(true);

echo "foreach: ";
echo $end - $start . PHP_EOL;

// FOR
$start = microtime(true);
$newData = array();
$numItems = count($data);
for ($i = 0; $i < $numItems; $i++) {
    if ($data[$i] % 2) {
        $newData[] = $data[$i];
    }
}
$end = microtime(true);

echo "for i: ";
echo $end - $start . PHP_EOL;
3pvhb19x

3pvhb19x3#

我知道这是个老问题,但我还是要给予:对我来说,使用foreach循环比使用array_filter快得多。使用foreach,按id搜索需要1.4秒,使用过滤器需要8.6秒。

hmae6n7t

hmae6n7t4#

根据我自己的经验,foreach更快。我认为这与函数调用开销,参数检查,复制到变量返回指令等有关。当使用基本语法时,解析的代码更可能更接近编译/解释的字节码,在核心下具有更好的优化。
通常的规则是:任何东西都更简单,运行更快(意味着更少的检查,更少的功能性,只要它有你需要的一切)

vd8tlhqk

vd8tlhqk5#

Array_Filter
迭代输入数组中的每个值,并将其传递给回调函数。如果回调函数返回true,则输入中的当前值将返回到结果数组中。数组键被保留。
我也一样。

相关问题