I know this is probably one of those "woods for the trees" questions but I've been sat here all day and I just cant see where Im going wrong.
The Scenario:
Basically I have a query returning the following assoc array of address results like so:
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 ) )
And all I'm trying to do is match these results against a search string to see if the address is already in my db.
my code is as follows:
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;
}
}
When I run this code I get the following output (sorry its a little long but the important bit is highlighted ):
bunnian place basingstoke rg21 7ej Current address: 23 abledone road hobs moat solihull west midlands b92 4jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 22 holdean road deepcut surrey kt164nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 32 palbrough road nottingham n32 4nj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: flat 2b gateway house rutland street bearwood westmidlands b66 9rt type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 258 lincoln street bullworth street dundee dd35 9rj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 4 scott arms avenue beckton hartfordshire h32 7jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 14 fairfield house millbank road bearwood birmingham westmidlands b137jj type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 9 scott road olton solihull westmidlands b91 7ty type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: normandy house bunnian place basingstoke rg21 7ej type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 3 new square bedfont lakes feltham middlesex tw14 8hb type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: no. 1 the square temple quay bristol bs1 6dg type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: the visual space capital park fulbourn cambridge cb21 5xh type of current address: string Search address: normandy house bunnian place basingstoke rg21 7ej Current address: 21 st. andrew square edinburgh scotland cb21 5xh type of current address: string -1
The strings highlighted are exactly the same, I've tested them by taking them both and sticking them in a separate if statement as plain strings and true is always returned . I've tried ==, === casting, string values and everything else I can think of but for some reason -1 is always returned. I know its probably something very simple Im missing but I just cant see it, can anyone help?
Thanks in advance.