我知道这可能是那些“树木换树”的问题之一,但我整天都坐在这里,我只是看不出我哪里出错了。
场景:
基本上我有一个查询返回以下地址结果的关联数组,如下所示:
Array ( [0] => Array ( [Address_ID] => 12 [Address_String] => 23 Abledone road Hobs moat Solihull West Midlands B92 4JJ ) [1] => Array ( [Address_ID] => 13 [Address_String] => 22 Holdean Road Deepcut Surrey KT164NJ ) [2] => Array ( [Address_ID] => 14 [Address_String] => 32 Palbrough Road Nottingham N32 4NJ ) [3] => Array ( [Address_ID] => 15 [Address_String] => Flat 2B Gateway House Rutland Street Bearwood Westmidlands B66 9RT ) [4] => Array ( [Address_ID] => 16 [Address_String] => 258 Lincoln Street Bullworth Street Dundee DD35 9RJ ) [5] => Array ( [Address_ID] => 17 [Address_String] => 4 Scott Arms Avenue Beckton Hartfordshire H32 7JJ ) [6] => Array ( [Address_ID] => 18 [Address_String] => 14 Fairfield house Millbank road Bearwood Birmingham Westmidlands B137JJ ) [7] => Array ( [Address_ID] => 19 [Address_String] => 9 Scott road Olton Solihull Westmidlands B91 7TY ) [8] => Array ( [Address_ID] => 20 [Address_String] => Normandy House Bunnian Place Basingstoke RG21 7EJ ) [9] => Array ( [Address_ID] => 27 [Address_String] => 3 New Square Bedfont Lakes Feltham Middlesex TW14 8HB ) [10] => Array ( [Address_ID] => 28 [Address_String] => No. 1 The Square Temple Quay Bristol BS1 6DG ) [11] => Array ( [Address_ID] => 29 [Address_String] => The Visual Space Capital Park Fulbourn Cambridge CB21 5XH ) [12] => Array ( [Address_ID] => 40 [Address_String] => 21 St. Andrew Square Edinburgh Scotland CB21 5XH ) )
我要做的就是将这些结果与搜索字符串进行匹配,以查看该地址是否已经在我的数据库中。
我的代码如下:
public function addressExistsByString ($searchAddrString, $asJSON = FALSE) {
if (!isset($searchAddrString)) {
throw new Exception('The searchAddrString variable of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . 'was empty or set to NULL.');
} elseif (!is_string($searchAddrString)) {
throw new Exception('The type of value for the argument addressID of function ' . __FUNCTION__ . ' of class ' . __CLASS__ . ' was ' . gettype($searchAddrString) . ' a string was expected');
}if (!is_bool($asJSON)) {
$asJSON = FALSE;
}
print_r ($addressArray = $this->getAllLocations(FALSE));
if ($addressArray === -1) {
return -2;
} else {
$search = str_replace(',','', strtolower(trim($searchAddrString)));
foreach ($addressArray as $record) {
$recID = $record ['Address_ID'];
$currentAddress =str_replace(',','', strtolower(trim($record['Address_String'])));
echo 'Search address: '. $search. ' Current address: '. $currentAddress .' type of current address: '.gettype($searchAddrString).'<br/>';
if ($currentAddress === $search) {
if ($asJSON) {
echo json_encode($recID);
}
return intval($recID);
}
}
if ($asJSON) {
echo json_encode(-1);
}
return -1;
}
}
当我运行此代码时,我得到以下输出(抱歉,它有点长,但重要的部分已突出显示):
bunnian place bassingstoke rg21 7ej 当前地址:23abledone road hobs moat solihull west midlands b92 4jj 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:22 holdean road deepcut surrey kt164nj 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:32 palbrough road nottingham n32 4nj 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:flat 2b gateway house rutland street Bearwood westmidlands b66 9rt 当前类型地址:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:258 lincoln street Bullworth street dudee dd35 9rj 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:4 scott arm avenue beckton hartfordshire h32 7jj 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:14 fairfield house millbank road Bearwood birmingham westmidlands b137jj 当前地址类型: string 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:9 scott road olton solihull westmidlands b91 7ty 当前地址类型:string 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:9 scott road olton solihull westmidlands b91 7ty 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:9 scott road olton solihull westmidlands b91 7ty 当前地址类型:字符串 搜索地址:诺曼底之家 bunnian place bassingstoke rg21 7ej当前地址:normandy house bunnian place bassingstoke rg21 7ej当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:3 new square bedfont lakes feelham middlesex tw14 8hb 当前地址类型:字符串 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:no. 1 the square Temple quay 布里斯托 bs1 6dg 当前地址类型:string 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:visual space capital park fulbourn cambridge cb21 5xh 当前地址类型:string 搜索地址:normandy house bunnian place bassingstoke rg21 7ej 当前地址:21 st。安德鲁广场爱丁堡苏格兰cb21 5xh当前地址类型:字符串-1
突出显示的字符串完全相同,我通过将它们都拿来测试它们并将它们作为纯字符串粘贴在单独的 if 语句中,并且总是返回 true 。我已经尝试过 ==、=== 转换、字符串值以及我能想到的所有其他内容,但由于某种原因,总是返回 -1。我知道这可能是我想念的非常简单的东西,但我就是看不到,有人可以帮忙吗?
提前致谢。