FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Need help accessing the key array.
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Need help accessing the key array. [message #185422 is a reply to message #185414] Sun, 30 March 2014 15:05 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Most secure way to reset a password via email link
Next Topic: Strange but true! Working with interfaces in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 16:44:30 GMT 2024

Total time taken to generate the page: 0.04330 seconds