Code: Select all
function searchArray($needle,$haystack)
{
$retKey = false;
while (list($key, $val) = each($haystack))
{
if( $val == $needle )
$retKey = $key;
echo $val.':'.$needle.'<br>';
}
return $retKey;
}
$blah = array(1,2,3,4,5,6,7,8,9,0);
if($key = searchArray(1,$blah))
echo $key.'<br>';
else
echo 'nomatch';The echo shows this.
1:1
2:1
3:1
4:1
5:1
6:1
7:1
8:1
9:1
0:1
nomatch
However If I use the same code but search for a value not in index zero;
Code: Select all
if($key = searchArray(2,$blah))
echo $key.'<br>';
else
echo 'nomatch';I get;
1:2
2:2
3:2
4:2
5:2
6:2
7:2
8:2
9:2
0:2
1 <-- being the index the function found the element
Any ideas as to what the hell is going on?