Well this seems to work for me just fine.

<?
$fruit = array('apple','banana','cranberry');
$findme = array('banana', 'cranberry', 'apple');

for ($i=0; $i<count($findme); $i++)
{
 $result = array_search($findme[$i], $fruit);
 if ($result !== false)
 {
  print "Key ($result) was found for value $findme[$i]<br>";
 }
 else
 {
  print "Sorry, could not find $findme[$i].<br>";
 }
}
?>

This code outputs:

Key (1) was found for value banana
Key (2) was found for value cranberry
Key (0) was found for value apple

-Kevin

----- Original Message -----
From: "andy" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 25, 2002 11:39 AM
Subject: Re: [PHP] how to make array_search start from 0?


> I can see what u are saying. Its all about the ===
>
> Anyhow.. this thing does not work in a loop. I guess this is my prob.
There
> might be something wrong with the boolean. I tryed it with different
> settings, but still wrong:
>
> Here is what I mean:
>
>   $fruit = array('apple','banana','cranberry');
>   $findme = array('apple', 'notlisted');
>
>   foreach($findme AS $value){
>    if (($key = array_search($value, $fruit)) !== false) {
>        print "Key ($key) was found from value $value<br>";
>    } else {
>        print "Sorry, $value was not found in array \$fruit<br>";
>    }
>   }
>
> Replys:
> Key (0) was found from value apple
> Key () was found from value notlisted
>
> So there must be still something wron in th stmt.
>
> Thanx for your help,
>
> Andy
>
> "Philip Olson" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
> Pine.BSF.4.10.10204251646060.3971-100000@localhost">news:Pine.BSF.4.10.10204251646060.3971-100000@localhost...
> > If the key is 0, array_search will return 0, it does not
> > start at 1.
> >
> >   $arr = array('apple','banana','cranberry');
> >   $key = array_search('apple', $arr);
> >
> >   print $key; // 0
> >
> > If 'apple' was not found, $key would then equal
> > to boolean false.  Be sure to use "=== false"
> > to check failure because 0 == false.  For example:
> >
> >   $fruit = array('apple','banana','cranberry');
> >   $findme = 'apple';
> >
> >   if (($key = array_search($findme, $fruit)) !== false) {
> >       print "Key ($key) was found from value $findme";
> >   } else {
> >       print "Sorry, $findme was not found in array \$fruit";
> >   }
> >
> > Again, remember, 0 == false.  == !=, === !==.  So, 0 !== false.
> > Wow that sounds confusing. :) Also consider the sexy array_keys()
> > function.
> >
> > Regards,
> > Philip Olson
> >
> > p.s. http://uk.php.net/manual/en/language.operators.comparison.php
> > p.s.s. also take into account extra whitespace (trim), and potential
> >        issues with case sensitivity (strtolower).
> >
> >
> > On Thu, 25 Apr 2002, andy wrote:
> >
> > > Hi there,
> > >
> > > I am passing an array through the URL with a ',' inbetween:
> > > var=php,mysql,super
> > > Parsing is done with: explode (',',$var). This gives me an array
> starting
> > > with 0
> > >
> > > Later on I have to search for lets say php with array_search.
> > >
> > > Unfortunatelly array_search requires an array starting with 1. So php
is
> not
> > > found.
> > >
> > > Does anybody know a workaround for this?
> > >
> > > Thanx,
> > >
> > > Andy
> > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> >
> >
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to