I’ve done a million if and foreach’s in scripts before, but this one I can’t seem to figure out. It just isn’t working…? Maybe it’s one of those things that if you look at it 100 times you won’t see the error and need a second set of eyes. Okay, so, here’s the code:
private function removeResultsByUID() {
foreach ($this->searchResults as $key => $value) {
if (!$this->searchResults[$key]['authorUID'] == $this->searchUID)
unset($this->searchResults[$key]);
}
return;
}
Simple enough, it is part of a forum search program that attempts to remove search results based on a username they might have entered to filter by. Problem is, it isn’t filtering, so I went into test mode and adjusted the code to see what was going on:
private function removeResultsByUID() {
foreach ($this->searchResults as $key => $value) {
var_dump($this->searchResults[$key]['authorUID']);
echo ' ';
var_dump($this->searchUID);
echo '<br />';
if (!$this->searchResults[$key]['authorUID'] == $this->searchUID) {
echo "This isn't a match";
unset($this->searchResults[$key]);
}
}
die();
return;
}
Okay, so a simple way to test, dump the variables I’m iffing to see what their values are set to (using var_dump instead of echo to ensure they are both string types for my knowledge). Then I add an echo to the if to see when the if condition triggers. Then kill the script to view the results. I toss in some search criteria with a username, the program changes it to a userID based on a table in the DB, every result it grabs from the search that matches adds the author’s user ID to the result array. Here is what I get:
string(1) "3" string(1) "3"
string(1) "1" string(1) "3"
string(1) "1" string(1) "3"
string(1) "1" string(1) "3"
It looks like the if is never triggering even though the dumped variables the if is using makes it seem as if it should trigger for the last 3 iterations. I’ve stared at it for an hour. What the hell did I do wrong? I’m going to kick myself for a stupid mistake I’m sure, I just can’t see it. Thanks for any help!
you want to use
!=and not!$x == $y:!$this->searchResults[$key]['authorUID']will either betrueorfalse(most likelyfalse, unless you have an id of “0”). so your comparison turns intofalse == $this->searchUID– not so likely to match.