我正在尝试在我的PHP Web应用程序中使用jQuery Autocomplete插件。 我在服务器上有一个JSON文件,其中包含搜索数据。它看起来像这样:
{
"_E161": {
"keggId":"rn:R05223",
"abbrev":"ADOCBLS",
"name":"Adenosylcobalamin 5'-phosphate synthase",
"equation":"agdpcbi[c] + rdmbzi[c] -> h[c] + adocbl[c] + gmp[c] ",
},
"_E163": {
....
}
}
我想通过这个带有PHP脚本的JSON文件(有3500个条目)来获取jQuery自动完成插件中的搜索词。然后将包含搜索词的条目返回到客户端以填充自动完成。 实现这个更好的方法是什么?我的第一个猜测是遍历JSON文件并使用strpos()但我怀疑这可能会很慢?
答案 0 :(得分:0)
您可以使用preg_grep(返回与模式匹配的数组条目),
// sanitize, and perform some processing to ensure is a valid regex pattern
$pattern = ...;
$json = json_decode( ... );
$arr = array();
foreach ($json as $key=>$arr)
{
$arr[$key] = $arr['name'];
}
$matches = preg_grep("/$pattern/i", $arr);
// $matches will hold the matches
// and you refer back to the $json using associate key