array_search troubles

array_search troubles

am 08.01.2008 22:05:26 von jpsebasti

Folks,

I'm having trouble interpreting the return value of the array_search
function. I'll give an example of my code and the output.

Basically, I'm trying to figure out how to check that no match was
found but no matter what I try checking on the return value, my code
keeps telling me that a match was found. I figured the array_search
is supposed to return the index key of the match so why can't I check
to see that the return value from array_search is >=0 and < the count
of the searched array? As you can see from this example, that does
not work? Any ideas of how to check the return value of
array_search()?


Here is the code:

$itemscols['COLUMN_NAME'][0] = 'CATNO';
$itemscols['COLUMN_NAME'][1] = 'FNAME';
$itemscols['COLUMN_NAME'][2] = 'MI';
$itemscols['COLUMN_NAME'][3] = 'LNAME';
$itemscols['COLUMN_NAME'][4] = 'PHONE';
$itemscols['COLUMN_NAME'][5] = 'EMAIL';
$itemscols['COLUMN_NAME'][6] = 'ADDR1';
$itemscols['COLUMN_NAME'][7] = 'ADDR2';
$itemscols['COLUMN_NAME'][8] = 'CITY';
$itemscols['COLUMN_NAME'][9] = 'STATE';
$itemscols['COLUMN_NAME'][10] = 'ZIP';
$itemscols['COLUMN_NAME'][11] = 'TITLE';
$itemscols['COLUMN_NAME'][12] = 'DESC';


$keys=array('CATNO','TITLE','LNAME');

var_dump($itemscols);
var_dump($keys);

foreach ($itemscols['COLUMN_NAME'] as $key)
{
$retval = array_search($key,$keys);
if ((int)$retval>=0) && (int)$retval {
echo "Found key $key! \$retval=$retval\n";
}
else
{
echo "Did not find key $key! \$retval=$retval\n";
}
}
?>


The output from the above script is:
array(1) {
["COLUMN_NAME"]=>
array(13) {
[0]=>
string(5) "CATNO"
[1]=>
string(5) "FNAME"
[2]=>
string(2) "MI"
[3]=>
string(5) "LNAME"
[4]=>
string(5) "PHONE"
[5]=>
string(5) "EMAIL"
[6]=>
string(5) "ADDR1"
[7]=>
string(5) "ADDR2"
[8]=>
string(4) "CITY"
[9]=>
string(5) "STATE"
[10]=>
string(3) "ZIP"
[11]=>
string(5) "TITLE"
[12]=>
string(4) "DESC"
}
}
array(3) {
[0]=>
string(5) "CATNO"
[1]=>
string(5) "TITLE"
[2]=>
string(5) "LNAME"
}
Found key CATNO! $retval=0
Found key FNAME! $retval=
Found key MI! $retval=
Found key LNAME! $retval=2
Found key PHONE! $retval=
Found key EMAIL! $retval=
Found key ADDR1! $retval=
Found key ADDR2! $retval=
Found key CITY! $retval=
Found key STATE! $retval=
Found key ZIP! $retval=
Found key TITLE! $retval=1
Found key DESC! $retval=

Re: array_search troubles

am 08.01.2008 22:12:48 von ivansanchez-alg

JP wrote:

> $retval = array_search($key,$keys);
> if ((int)$retval>=0) && (int)$retval
On failure, $retval is the boolean "false". You're casting it into an
integer, thus becoming 0 for purposes of the evaluation. As 0 >= 0, and 0
< count($keys), it doesn't work as you expect.

Now, go back to http://php.net/array_search and carefully read the warning
in the reddish box.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: De-Phazz - Daily Lama (2002) - [18] Style (4:51)
(97.222198%)

Re: array_search troubles

am 08.01.2008 22:31:29 von jpsebasti

Thanks for the reply:

I had read that box and now have re-read it and am still confused.
The warning states:

"Warning

This function may return Boolean FALSE, but may also return a non-
Boolean value which evaluates to FALSE, such as 0 or "". Please read
the section on Booleans for more information. Use the === operator for
testing the return value of this function."

This warning says to me that I cannot use something like
if (array_search($key,$keys))
{...}

because match on the array index [0] which would return 0 is similar
to returning FALSE.

Upon further consideration however, I decided to do something like
this (which works)

$retval = array_search($key, $keys);
if ($retval)
{
echo "Match found\n";
}
else
{
if ($retval === 0)
{
echo "Match found\n";
}
else
{
echo "Match not found\n";
}
}


So it appears that if a FALSE is returned by the function but the 0
return value really means it matched key [0] in the haystack, you
should check for this with the === operator.

Re: array_search troubles

am 09.01.2008 02:47:25 von Vince Morgan

"JP" wrote in message
news:d39735da-1162-43b5-8748-c7382d675387@q39g2000hsf.google groups.com...
> Thanks for the reply:
>
> I had read that box and now have re-read it and am still confused.
> The warning states:
>
> "Warning
>
> This function may return Boolean FALSE, but may also return a non-
> Boolean value which evaluates to FALSE, such as 0 or "". Please read
> the section on Booleans for more information. Use the === operator for
> testing the return value of this function."
>
> This warning says to me that I cannot use something like
> if (array_search($key,$keys))
> {...}
>
> because match on the array index [0] which would return 0 is similar
> to returning FALSE.
>
> Upon further consideration however, I decided to do something like
> this (which works)
>
> $retval = array_search($key, $keys);
> if ($retval)
> {
> echo "Match found\n";
> }
> else
> {
> if ($retval === 0)
> {
> echo "Match found\n";
> }
> else
> {
> echo "Match not found\n";
> }
> }
>
>
> So it appears that if a FALSE is returned by the function but the 0
> return value really means it matched key [0] in the haystack, you
> should check for this with the === operator.
>
Yes.

if($retval === false)
{
echo "Not found";
}else
{
echo "Yep, got it";
}

if($retval !== false)
{
echo "Yep, got it";
}else
{
echo "Not found";
}

Is more compact.
Vince