我有两张 table 。一个有大量数字的人。然后我有另一个表,其中包含前缀列表(30, 000+)。
我需要循环遍历前缀并查看表 1 中的任何数字是否以任何前缀开头。
这就是我到目前为止所拥有的。
$tdata = $r->get_t_data(); //array of prefix
/*
Array
(
[0] => Array
(
[prefix] => 101
[dest] => UK
)
)
*/
$cdata = $r->get_c_data(); //array of number
/*Array
(
[0] => Array
(
[row] => 1
[num] => 441143610120
)
)*/
$temp = array();
$i=0;
$time=0;
foreach ($cdata as $ckey => $c) {
foreach ($tdata as $tkey => $t) {
$length = strlen($t['prefix']);
if (strpos($c['num'], $t['prefix'], 0, $length )) {
$temp[$i]['row']=$c['row'];
$temp[$i]['prefix']=$t['prefix'];
$temp[$i]['dialled']=$c['num'];
$temp[$i]['dest']=$t['dest'];
break;
$i++; //increment only if found
}
}
$time++;
}
所以基本上它循环遍历数字,然后我尝试将数字的第一部分与前缀匹配。
目前它正在返回空数组。
希望能帮到你
最佳答案
最好的办法是在 sql 中进行连接,而不是在 PHP 中进行检查。要与类似的对象进行连接,您可以执行以下操作:
SELECT * FROM table t JOIN prefixTable p ON t.num LIKE CONCAT(p.prefix, '%')
关键是LIKE CONCAT(p.prefix, '%')
,也就是说合并t.num
类似于prefix%
的表>在 MySQL 中 %
是一个通配符,因为我们没有在前面放置通配符,这意味着 t.num
列必须以前缀开始
关于php - 如何将大量字符串与另一个字符串的开头进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472583/