[英]Matching items in two arrays
Right i've got two arrays that i'm trying to compare to see if words match between them. 正确,我有两个数组,我正在尝试比较它们之间的单词是否匹配。
$address = explode(",", '31 Birmingham Road, Erdington, Birmingham, Blah');
I'm using the following which doesn't find any matches however when exploring the loop I can get Erdington = Erdington and it still returns no match? 我正在使用以下找不到任何匹配项的内容,但是在探索循环时,我可以得到Erdington = Erdington,但仍未返回任何匹配项?
if (count($_POST['location'])) {
foreach ($_POST['location'] as $value) {
if (in_array($value, $address)) {
$exists = 1;
$success[] = "Match";
}
}
}
Any ideas? 有任何想法吗?
The space after the comma is causing the test to fail. 逗号后的空格导致测试失败。 If the addresses will always have that space you can use @SharpEdge's answer.
如果地址将始终具有该空间,则可以使用@SharpEdge的答案。 If it's optional, use
trim()
如果是可选的,请使用
trim()
$address = array_map('trim', explode(",", '31 Birmingham Road, Erdington, Birmingham, Blah'));
It's possible that the space after the comma is causing the condition to fail. 逗号后的空格可能导致条件失败。
' Erdington' != 'Erdington'
Dirty test (adding a space in the delimiter after the comma) 脏测试(在逗号后的定界符中添加空格)
$address = explode(", ", '31 Birmingham Road, Erdington, Birmingham, Blah');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.