Ok, im doing somthing fairly retuine here but for some reason, when search my array using my own function or the built in function... It never returns true if the element I am search for is sitting in Index zero.

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 an value not in index zero like, 2;

 

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?

    It never returns true if the element I am search for is sitting in Index zero.

    since you are returning the position of the element you cant expect your function to evaluate as true when the element is found at position 0 of the array... agree ?

    You might want to look the manual for array_search and take a look at the warning a the end of the man page.

    Hope that helps,

    a

      Yes i have read the man pages on the array_search function ..

      But my point being;

      if( 1 == array[0] )

      should return 0;

      There is no reason why you should not be able to test against the first element in an array .

        ignore that .. im a dumb ass ..

        zero same as false .. i getcha

          you got the point 🙂 mark it as resolved if everything is ok now.

            FYI, if you want to see if something really is false (and not just equivalent to false), you can use === to test it instead of == (the opposite of === is !==).

              Write a Reply...