RE: [PHP] Array question
| From: | Ford, Mike | Date: | Tue, 27 Feb 2007 10:37:16 +0000 |
| Subject: | RE: [PHP] Array question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-249483@lists.php.net to get a copy of this message | ||
On 27 February 2007 04:23, Gerry D wrote:
> 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'...
Please show a little more code, as it looks to me as though this should work how you think it
should.
Specifically: how do we know what is in $c? how do you know the array_search works? how do you know
$n is only getting 'a', 'b', or 'c'? (Hint: var_dump() is your
friend!)
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm