PHP in_array () cannot even match a single character. String is set to true
I've seen a million of these streams already and am reading every one of them. That, plus some serious Googling.
UPDATE: I am rewriting this post to include the complete code and explanation so that everyone understands what is going on and what I am trying to do.
I am developing with CodeIgniter, so some of the syntax may look strange if you are not familiar with it.
I have a string of links with the letters AZ. The idea is to find only "active" letters containing content in a specific column (mysql LIKE $letter%
). With this information, I could "flatten" some "blank" letters, if any, using CSS.
This function here queries mysql and gets every unique first letter of the records in the column. The result should be between 0 and 26 matches / array elements.
//From am_model.php
function getFirstLetter($domainId)
{
$q = $this->db->query("SELECT DISTINCT LEFT(alias_name, 1)
AS letter
FROM am_aliases
WHERE domain_id = '" . $domainId . "'
ORDER BY alias_name;");
if($q->num_rows > 0):
foreach($q->result() as $row)
{
$result[] = $row;
}
//print_r($result); <-- prints out correct result.
return $result;
endif;
}
After that, I call this function from the controller:
$foundLetters = $this->am_model->getFirstLetter($domainId);
then define an array $alphabet
.
$alphabet = range('a','z');
foreach($alphabet as $letter)
{
if(in_array($letter, $foundLetters, TRUE)):
echo $letter . ' found<br />';
else:
echo $letter . ' not found<br />';
endif;
}
Nothing complicated. All I have to do is check if one character in the loop matches for my alphabetical array.
As Col. Shrapnel, I did some debugging, and the dump()
emails from the $ alphabet and $ foundLetters arrays gave different results, so I think this points to possible coding issues that I am trying to figure out now ...
Does anyone know what the hell is going on here?
function dump(&$str) {
$i=0;
while (isset($str[$i])) echo strtoupper(dechex(ord($str[$i++])));
}
Here is the output from dump()
:
a: $alphabet->61 613C6272202F3E<-$foundLetters
b: $alphabet->62 613C6272202F3E<-$foundLetters
c: $alphabet->63 683C6272202F3E<-$foundLetters
d: $alphabet->64 613C6272202F3E<-$foundLetters
and they:
print_r($alphabet); // all 26 letters
Array (
[0] => a
[1] => b
[2] => c
...
[23] => x
[24] => y
[25] => z
)
print_r($foundLetters); // dynamic array.
Array (
[0] => b
[1] => s
)
a source to share
got your emails from a file, huh? :)
use var_dump
instead or print_r and trim in comparison :)
Edit
Use this code to see what's going on.
foreach ($alphabet as $letter) {
foreach ($empty_letters as $empty) {
dump($letter);
echo " ";
dump($empty);
echo "<br>";
if ($letter == $empty) {
echo "$letter was found in \$empty_letters<br>\n";
break;
}
}
}
function dump(&$str) {
$i=0;
while (isset($str[$i])) echo strtoupper(dechex(ord($str[$i++])));
}
a source to share
I got the expected results. But I think your example array is different from what you are actually passing through. Try it...
foreach(array_values($alphabet) as $letter){
echo $letter . '<br />'; // Correctly prints out every letter from $alphabet.
if(in_array($letter, $emptyLetters)) { // $strict is set
// do something
echo 'found';
}
}
a source to share