Re: Searching for array keys

From: Date: Fri, 04 May 2001 13:38:57 +0000
Subject: Re: Searching for array keys
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-51412@lists.php.net to get a copy of this message
Mark Charette wrote: > >> > What's the best way of finding out if a specific array key is in an > array? >> > I have an associative array which *may* look like >> > (car =>saab, house => mansion, countyW=> A) but can equally well look > like >> > >> > (boat => daycruiser, house => flat, county => B). >> > >> > I want to find out if the key "car" is in the array and do something if > it >> try something like >> >> if >> (in_array("keyb",array_keys(array("key"=>"a","keyb"=>"b"))))) >> { >> ... >> } > > Maybe I'm being dense here, but won't a simple "isset()" work (using the > KISS principle)? > > $ary=array("aa"=>"yy","bb"=>"zz"); > > $key="aa"; > if (isset($ary[$key])) > print "$key set\n"; > else > print "$key not set\n"; > > $key="bb"; > if (isset($ary[$key])) > print "$key set\n"; > else > print "$key not set\n"; > > $key="cc"; > if (isset($ary[$key])) > print "$key set\n"; > else > print "$key not set\n" > > returns > > aa set > bb set > cc not set > Thanks, So simple - giving it more thought than before rushing off to work I would possibly have found it myself. M.

« previous php.general (#51412) next »