我声明了以下数组:
$array = array(
"Car" => array("(car;", "(is Car;", "(suv;"),
"Boat" => array("(boat;", "(Boat;"),
"Motorcycle" => array("(cycle;")
);
然后我查询MySQL数据库大约20,000个结果作为字符串($string
),所以我想要做的是搜索子数组值(即“(car;”)在其中一个字符串中类似于“真棒(汽车;越野;)”
为实现这一目标,我尝试了以下方法:
foreach($string as $element) {
foreach($array as $key=>type) {
foreach($type as $search_string) {
if(strpos($element, $search_string) !== FALSE) { //if the search term is present
return $key;
break 2; //exit the loop as it found the right device type
}
} //second foreach strings to search for
} return 'Other';
}
如果我查询的结果非常少,问题是它有效,但是对于数千行,它只消耗大量内存,并且返回结果需要很长时间。
如何提高代码的性能?