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 #185415 is a reply to message #185414] Sat, 29 March 2014 23:24 Go to previous messageGo to previous message
Richard Damon is currently offline  Richard Damon
Messages: 58
Registered: August 2011
Karma:
Member
If you store the data the way you are, the only way to find a record is
by searching through the array (there are ways to get PHP do the
searching, but it still requires searching).

An alternative data organization is to make an array of arrays that you
index with the animal and color values to get the rate. Something like:

$rates = array( // Animals
0 => array ( // Colors => Rate
0 => 5,
1 => 10,
2 => 15,
),
1 => array ( // Colors => Rate
0 => 20,
1 => 25,
2 => 30,
),
....
);


On 3/29/14, 6:32 PM, 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?
>
> Thanks.
>
> On Sunday, March 30, 2014 4:05:41 AM UTC+7, Jerry Stuckle wrote:
>> On 3/29/2014 4:49 PM, Kongthap Thammachat wrote:
>>
>>> <?php
>>
>>>
>>
>>> $rates = array(
>>
>>> array("animal" => 0, "color" => 0, "rate" => 5),
>>> array("animal" => 0, "color" => 1, "rate" => 10),
>>> array("animal" => 0, "color" => 2, "rate" => 15),
>>> array("animal" => 1, "color" => 0, "rate" => 20),
>>> array("animal" => 1, "color" => 1, "rate" => 25),
>>> array("animal" => 1, "color" => 2, "rate" => 30),
>>> array("animal" => 2, "color" => 0, "rate" => 35),
>>> array("animal" => 2, "color" => 1, "rate" => 40),
>>> array("animal" => 2, "color" => 2, "rate" => 45),
>>> array("animal" => 3, "color" => 0, "rate" => 50),
>>> array("animal" => 3, "color" => 1, "rate" => 55),
>>> array("animal" => 3, "color" => 2, "rate" => 60)
>>> );
>>>
[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 17:30:53 GMT 2024

Total time taken to generate the page: 0.04247 seconds