Re: [PHP4BETA] in_array

From: Date: Wed, 20 Oct 1999 17:34:51 +0000
Subject: Re: [PHP4BETA] in_array
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-5611@lists.php.net to get a copy of this message
David Hjortso/ wrote: > Hi, > > I don't know wether this has been asked before, and wether it has been > "added", but I would like in_array to return the cursor for the array that > is == the variable you asked for. > > This would spare a lot of time. Having to do a > if(in_array("y",$array)){ for(..){ if($array[$i] == > "y"){ unset($array[$i]); } } > instead of > $arraytest = in_array("y",$array); > f($arraytest){ unset($array[$arraytest]); } > > (Think of an array having 100's even 1000's of keys) > > As someone in the Online manual put it: > you have a haystack; you have a needle you want to find; you ask your buddy > in_array if he knows if your needle is in the haystack; he looks in the > haystack, turns to you and answers "yes it is" so you look at the haystack > which is several miles high, wide, and deep and ask him "where in the > haystack ?" and he says ".... you dont need to know, try asking someone > else" That's pretty good. :) Online manual you say? Seriously though, in_array() should return true or false. It's not really possible to return the reference to the result and then it all becomes messy since if it's found you get result if it's not found you get false. Which version of PHP are you using? If PHP4 then Beta 3 has a function called array_keys() which can be used to do what you want. Example: $res = array_keys($array, "y"); Then $res is an array of keys corresponding to 'y' entries in the source array. -Andrei * Proximity bug: when the code crashes in front of important visitors. *

« previous php.version4 (#5611) next »