gpt4 book ai didi

php - 搜索数组: array_filter vs loop

转载 作者:行者123 更新时间:2023-12-05 00:04:48 24 4
gpt4 key购买 nike

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

最佳答案

Array_Filter

Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.



对我来说也是一样

关于php - 搜索数组: array_filter vs loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6791479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com