Re: Array question

From: Date: Tue, 27 Feb 2007 04:29:38 +0000
Subject: Re: Array question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-249476@lists.php.net to get a copy of this message
Try: $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); $search = 'apple'; foreach($fruit as $key => $val) { if ($val == $search) {
    echo "$search found: array key is $key, value is $val";
} } 2007/2/27, Gerry D <gdanen.spam@gmail.com>:
I have a question on how to retrieve the value that corresponds to a key in an array. $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
                $key = array_search($c, $fruit);
                if ( $key === FALSE )
                        $n = $c;
                else
                {
                        $n = $fruit[$key];  // how to get the value???
                }
the array_search works ok, but how do I get the value? all I get back is 'a' or 'b', not 'apple' or 'banana'... TIA Gerry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- -- Hap-Hang Yu, Jay

« previous php.general (#249476) next »