Re: Need help accessing the key array. [message #185422 is a reply to message #185414] |
Sun, 30 March 2014 15:05 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma:
|
Senior Member |
|
|
Kongthap Thammachat wrote:
> Hi, what I am trying to do is to receive animal variable and color
> variable from the user, and finally display associated rate for the
> input animal and input.
>
> I'm looking for any ways rather than looping through the $rates
> array, is there any possible ways?
If the data is as uniform as you have posted, you don't even need an
array, but you can use the following function instead:
function rate($animal, $color) {
return 5 * (3*$animal + $color + 1);
}
If the data you've posted were only an example, and the rates couldn't
be calculated as easily as above, you might be better off to change the
array structure, e.g.
$rates = array(
"animal0-color0" => 5,
"animal0-color1" => 10,
"animal0-color2" => 15,
"animal1-color1" => 20,
// ...
);
function rate($animal, $color) {
global $rates;
return $rates["animal$animal-color$color];
}
And please do not top-post, and avoid posting lines longer than 72 or 76
characters (Google Groups can't properly handle them).
--
Christoph M. Becker
|
|
|