我知道这可能就是那些“树木问候”的问题之一,但我整天坐在这里,我只是看不出我错了。
情景:
基本上我有一个查询返回以下关联的地址结果数组,如下所示:
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 basingstoke rg21 7ej当前地址:23 abledone road hobs moat solihull west midlands b92 4jj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:22 holdean road deepcut surrey kt164nj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:32 palbrough road nottingham n32 4nj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:flat 2b gateway house rutland street bearwood westmidlands b66 9rt类型当前地址:string 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:258 lincoln street bullworth street dundee dd35 9rj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:4 scott arms avenue beckton hartfordshire h32 7jj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:14 fairfield house millbank road bearwood birmingham westmidlands b137jj当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:9 scott road olton solihull westmidlands b91 7ty当前地址类型:字符串 搜索地址: normandy house bunnian place basingstoke rg21 7ej 当前地址: normandy house bunnian place basingstoke rg21 7ej 当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:3 new square bedfont lakes feltham middlesex tw14 8hb当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:no。 1方形寺码头布里斯托尔bs1 6dg当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:visual space capital park fulbourn cambridge cb21 5xh当前地址类型:字符串 搜索地址:normandy house bunnian place basingstoke rg21 7ej当前地址:21 st。安德鲁广场爱丁堡苏格兰cb21 5xh当前地址类型:字符串 -1
突出显示的字符串完全相同,我测试了它们并将它们作为普通字符串粘贴在单独的if语句中,并且始终返回true。 我已经尝试了==,===强制转换,字符串值以及我能想到的其他所有内容,但由于某种原因,始终会返回-1。 我知道它可能是非常简单的我想念但是我无法看到它,任何人都可以帮忙吗?
提前致谢。
答案 0 :(得分:1)
var_dump()你的两个变量。在此之前回显<pre>
标记,以查看浏览器中的所有空格,而无需切换到HTML源代码。
你很可能会错过那里的一些空白角色。