I’m having trouble understanding why in_array() won’t work for me.
I have a long string of names separated by :: which I’ve placed in a variable $someWords
I use $wordChunks = explode(“::”, $someWords); to explode the string into an array.
I then want to use in_array() to see if a word is in $wordChunks, but using a word that I know is in the array, I’m told it isn’t…
Here’s my test case:
$wordChunks = explode("::", $someWords);
echo 'The contents of $wordChunks[1219] is: '.$wordChunks[1219].'<br>';
echo is_array($wordChunks) ? '$wordChunks is an array' : '$wordChunks is not an Array';
echo "<br>";
if (in_array("zaba",$wordChunks))
{
echo "Match found";
}
else
{
echo "Match not found";
}
Using this code I’m told that:
The contents of $wordChunks[1219] is: zaba
$wordChunks is an array
Match not found
So I DO have an array, my word IS in it, but in_array() says it isn’t…? What am I doing wrong please? I’ve looked at the php man pages and googled it but can’t see where my logic is wrong.
Thanks
Can you show the value of $someWords please?
Also can you provide a var_dump of $wordChunks too.
Also try setting strict to true - can’t guarantee it will work but I’ve heard some rumours about it. I also had a problem with this function myself a long while back.
Tried if (in_array(“zaba”,$wordChunks,true)) … No change.
$someWords is a massive list of members names, I won’t post it here, but should make a small ‘test’ list next.
Aaaah… But…
var_dump tells me: [1219]=> string(5) " zaba" and, looking at all the other array values, all seem to have a leading, non displaying character, so I’m thinking that this is the reason that in_array() isn’t making the match.
Using Gedit (Linux) or Notepad++ (Win) doesn’t show those extra leading character(s), I’ll take a look at the file I was given with a hex editor (Octeta) and see what is lurking there.:mad:
I’ll try trim() on the file and see what it ‘whacks’…
Thanks!
trim() worked… From sample at: http://php.net/manual/en/function.trim.php
list here...
$wordChunks = explode("::", $someWords);
function trim_value(&$value)
{
$value = trim($value);
}
//var_dump($wordChunks);
array_walk($wordChunks, 'trim_value');
//var_dump($wordChunks);
if (in_array('zaba',$wordChunks))
{
echo "Match found";
}
else
{
echo "Match not found";
}
Now prints: Match found
Mahalo nui loa!