in_array问题

Jam*_*mes 3 php arrays search

我有几个大数组作为项目,每个数组有5 000 - 10 000个值.

所有都是简单的数组,比如

$array = array(125,345345,345,3485,324,65,746647,3221, ... );

我正在尝试搜索一些数字,并对不同的数字重复此操作近1000次.

喜欢

if $array has item 345 {
    return true
} else {
    return false
}
Run Code Online (Sandbox Code Playgroud)

但要求需要很长时间才能完成.有时服务器会发出超时错误.

在简单的结构中搜索某个数字的最佳方法是什么,但是它们的大小数组很大?

Mat*_*hew 8

The simplest thing is to flip the array around (see array_flip) and use isset($array[$key]). That uses a hash lookup instead of a search, so it's much faster.

Other than that, try using a database or some more optimal way of dealing with large datasets.