Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13283 > unrolled thread

Need help accessing the key array.

Started byKongthap Thammachat <kongthap@gmail.com>
First post2014-03-29 13:49 -0700
Last post2014-03-31 02:44 +0000
Articles 10 on this page of 30 — 9 participants

Back to article view | Back to comp.lang.php


Contents

  Need help accessing the key array. Kongthap Thammachat <kongthap@gmail.com> - 2014-03-29 13:49 -0700
    Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-29 17:05 -0400
      Re: Need help accessing the key array. Kongthap Thammachat <kongthap@gmail.com> - 2014-03-29 15:32 -0700
        Re: Need help accessing the key array. Richard Damon <Richard@Damon-Family.org> - 2014-03-29 19:24 -0400
        Re: Need help accessing the key array. "J.O. Aho" <user@example.net> - 2014-03-30 00:39 +0100
        Re: Need help accessing the key array. Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-30 17:05 +0200
        Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-30 13:14 -0400
          Re: Need help accessing the key array. "J.O. Aho" <user@example.net> - 2014-03-31 07:30 +0200
            Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 07:37 -0400
              Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-01 00:55 +0000
                Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 21:09 -0400
                Re: Need help accessing the key array. Richard Damon <Richard@Damon-Family.org> - 2014-03-31 22:16 -0400
                  Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-01 20:32 +0000
    Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-30 01:08 +0000
    Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-30 15:40 -0700
      Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-30 19:55 -0400
        Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-30 20:15 -0700
          Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 07:35 -0400
            Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-31 06:26 -0700
              Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 10:33 -0400
                Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-02 06:47 -0700
                  Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-02 10:51 -0400
                    Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-02 09:57 -0700
                      Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-02 13:25 -0400
                        Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-03 14:20 -0700
                          Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-03 21:11 -0400
                      Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-04 02:13 +0000
                  Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-02 17:21 +0000
    Re: Need help accessing the key array. Norman Peelman <npeelman@cfl.rr.com> - 2014-03-30 22:41 -0400
    Re: Need help accessing the key array. Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-31 02:44 +0000

Page 2 of 2 — ← Prev page 1 [2]


#13310

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-04-02 06:47 -0700
Message-ID<a3c20161-fe6d-4944-93e3-e288f941bd9c@googlegroups.com>
In reply to#13303
On Monday, March 31, 2014 9:33:44 AM UTC-5, Jerry Stuckle wrote:
> On 3/31/2014 9:26 AM, Kevin Burton wrote:
> 
> > On Monday, March 31, 2014 6:35:41 AM UTC-5, Jerry Stuckle wrote:
> 
> 
> 
> >>
> 
> >>>> A decent idea, but much harder than it needs to be.  A simple two
> 
> >>>> dimensional array suffices for what he needs.  See the other updates in
> 
> >>>> this thread.
> 
> >>
> 
> >>> In order for a 2D array to work you have to change the input array and the input parameters. Right?
> 
> >>
> 
> >> Nope.  Just make the array itself two dimensional.  A lot easier than
> 
> >> all of your code (and a lot fewer changes, also).
> 
> >
> 
> > But what if you can't change the input array?
> 
> >
> 
> 
> 
> What input array?  The array is generated in the code, and the indicies 
> 
> are coming from the user - probably via either $_GET or $_POST values.
> 
> 
> 
> He is generating this array in his code to hold the values, and needs to 
> 
> only index properly into the array to get the result he wants.  No 
> 
> searching or classes needed, i.e. (after proper filtering, of course)
> 
> 
> 
> $rate = $rates[$_POST['animal']][$_POST['color']];
> 
> 
> 
> Much easier to understand and code.
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

I guess I still don't understand. The input array is:

$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) 
); 

If $input_animal is 1 then $rates[1] will be array("animal" => 0, "color" => 1, "rate" => 10). If $input_color = 2 then $rates[1][2] will be 10. Right?

[toc] | [prev] | [next] | [standalone]


#13311

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-04-02 10:51 -0400
Message-ID<lhh869$1dl$1@dont-email.me>
In reply to#13310
On 4/2/2014 9:47 AM, Kevin Burton wrote:
> On Monday, March 31, 2014 9:33:44 AM UTC-5, Jerry Stuckle wrote:
>> On 3/31/2014 9:26 AM, Kevin Burton wrote:
>>
>>> On Monday, March 31, 2014 6:35:41 AM UTC-5, Jerry Stuckle wrote:
>>
>>
>>
>>>>
>>
>>>>>> A decent idea, but much harder than it needs to be.  A simple two
>>
>>>>>> dimensional array suffices for what he needs.  See the other updates in
>>
>>>>>> this thread.
>>
>>>>
>>
>>>>> In order for a 2D array to work you have to change the input array and the input parameters. Right?
>>
>>>>
>>
>>>> Nope.  Just make the array itself two dimensional.  A lot easier than
>>
>>>> all of your code (and a lot fewer changes, also).
>>
>>>
>>
>>> But what if you can't change the input array?
>>
>>>
>>
>>
>>
>> What input array?  The array is generated in the code, and the indicies
>>
>> are coming from the user - probably via either $_GET or $_POST values.
>>
>>
>>
>> He is generating this array in his code to hold the values, and needs to
>>
>> only index properly into the array to get the result he wants.  No
>>
>> searching or classes needed, i.e. (after proper filtering, of course)
>>
>>
>>
>> $rate = $rates[$_POST['animal']][$_POST['color']];
>>
>>
>>
>> Much easier to understand and code.
>>
>>
>
> I guess I still don't understand. The input array is:
>
> $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)
> );
>
> If $input_animal is 1 then $rates[1] will be array("animal" => 0, "color" => 1, "rate" => 10). If $input_color = 2 then $rates[1][2] will be 10. Right?
>

Yes, but this is not what we are suggesting.  Please see the earlier 
updates.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13312

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-04-02 09:57 -0700
Message-ID<9e3609f4-23d9-4dfa-bb1f-a929f242b101@googlegroups.com>
In reply to#13311
That is what I meant when I said what if you cannot change the input. You replied, "what input?". The input was the array that was specified in the original question.

On Wednesday, April 2, 2014 9:51:56 AM UTC-5, Jerry Stuckle wrote:
> On 4/2/2014 9:47 AM, Kevin Burton wrote:
> 
> > On Monday, March 31, 2014 9:33:44 AM UTC-5, Jerry Stuckle wrote:
> 
> >> On 3/31/2014 9:26 AM, Kevin Burton wrote:
> 
> >>
> 
> >>> On Monday, March 31, 2014 6:35:41 AM UTC-5, Jerry Stuckle wrote:
> 
> >>
> 
> >>
> 
> >>
> 
> >>>>
> 
> >>
> 
> >>>>>> A decent idea, but much harder than it needs to be.  A simple two
> 
> >>
> 
> >>>>>> dimensional array suffices for what he needs.  See the other updates in
> 
> >>
> 
> >>>>>> this thread.
> 
> >>
> 
> >>>>
> 
> >>
> 
> >>>>> In order for a 2D array to work you have to change the input array and the input parameters. Right?
> 
> >>
> 
> >>>>
> 
> >>
> 
> >>>> Nope.  Just make the array itself two dimensional.  A lot easier than
> 
> >>
> 
> >>>> all of your code (and a lot fewer changes, also).
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> But what if you can't change the input array?
> 
> >>
> 
> >>>
> 
> >>
> 
> >>
> 
> >>
> 
> >> What input array?  The array is generated in the code, and the indicies
> 
> >>
> 
> >> are coming from the user - probably via either $_GET or $_POST values.
> 
> >>
> 
> >>
> 
> >>
> 
> >> He is generating this array in his code to hold the values, and needs to
> 
> >>
> 
> >> only index properly into the array to get the result he wants.  No
> 
> >>
> 
> >> searching or classes needed, i.e. (after proper filtering, of course)
> 
> >>
> 
> >>
> 
> >>
> 
> >> $rate = $rates[$_POST['animal']][$_POST['color']];
> 
> >>
> 
> >>
> 
> >>
> 
> >> Much easier to understand and code.
> 
> >>
> 
> >>
> 
> >
> 
> > I guess I still don't understand. The input array is:
> 
> >
> 
> > $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)
> 
> > );
> 
> >
> 
> > If $input_animal is 1 then $rates[1] will be array("animal" => 0, "color" => 1, "rate" => 10). If $input_color = 2 then $rates[1][2] will be 10. Right?
> 
> >
> 
> 
> 
> Yes, but this is not what we are suggesting.  Please see the earlier 
> 
> updates.
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

[toc] | [prev] | [next] | [standalone]


#13314

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-04-02 13:25 -0400
Message-ID<lhhh6d$ctc$1@dont-email.me>
In reply to#13312
On 4/2/2014 12:57 PM, Kevin Burton wrote:
> That is what I meant when I said what if you cannot change the input. You replied, "what input?". The input was the array that was specified in the original question.
>

The array is not "input".  Input comes from an outside source.  The 
array was generated by the OP in his code.

It's his code.  He can change it any way he wants.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13322

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-04-03 14:20 -0700
Message-ID<33fd2117-1a92-4af1-bbdb-83cd2380d52c@googlegroups.com>
In reply to#13314
Where does he say the the array was generated by an outside source specifically 'OP' in his code? He says:

       How to access the $rates array to echo the associated rate of
       "animal" => 1 and "color" => 2 ? (which is 30),
       should i re-designed the $rates array?

And

       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 Wednesday, April 2, 2014 12:25:36 PM UTC-5, Jerry Stuckle wrote:
> On 4/2/2014 12:57 PM, Kevin Burton wrote:
> 
> > That is what I meant when I said what if you cannot change the input. You replied, "what input?". The input was the array that was specified in the original question.
> 
> >
> 
> 
> 
> The array is not "input".  Input comes from an outside source.  The 
> 
> array was generated by the OP in his code.
> 
> 
> 
> It's his code.  He can change it any way he wants.
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

[toc] | [prev] | [next] | [standalone]


#13323

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-04-03 21:11 -0400
Message-ID<lhl0r8$f2r$1@dont-email.me>
In reply to#13322
On 4/3/2014 5:20 PM, Kevin Burton wrote:
> Where does he say the the array was generated by an outside source specifically 'OP' in his code? He says:
>
>         How to access the $rates array to echo the associated rate of
>         "animal" => 1 and "color" => 2 ? (which is 30),
>         should i re-designed the $rates array?
>
> And
>
>         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.
>

I didn't claim he did.  YOU did.  I agreed with others he should change 
the format of the array.  You're the only one who created a bunch of 
unnecessary code to complicate a simple process.

And don't top post.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13324

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-04-04 02:13 +0000
Message-ID<lhl4gd$n9l$4@dont-email.me>
In reply to#13312
On Wed, 02 Apr 2014 09:57:15 -0700, Kevin Burton wrote:

> That is what I meant when I said what if you cannot change the input.
> You replied, "what input?". The input was the array that was specified
> in the original question.

No, the array that was specified in the original question was not input, 
although it could be described as data held within the program that.

Input is information that is provided to the program at run time. Not 
data that is part of the program.

In the problem as originally posted, all the data was contained within 
the program code. There was no separate input data.

The answer to the first question the OP asked (How to access the $rates 
array to echo the associated rate of "animal" => 1 and "color" => 2 ?) is:

$rates[3*$input_animal+$input_color]["rate"]

The answer to the second question the OP asked (should i re-designed the 
$rates array) is:

Yes, there are much better ways to design your data array.

The answer to "Does Kevin Burton respect usenet etiquette" is clearly:

No, he can't be bothered to respect nearly 35 years of usenet etiquette.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#13313

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-04-02 17:21 +0000
Message-ID<lhhgu5$bna$1@dont-email.me>
In reply to#13310
On Wed, 02 Apr 2014 06:47:02 -0700, Kevin Burton wrote:

> I guess I still don't understand. The input array is:
> 
> $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)
> );
> 
> If $input_animal is 1 then $rates[1] will be array("animal" => 0,
> "color" => 1, "rate" => 10). If $input_color = 2 then $rates[1][2] will
> be 10. Right?

Let's look at how you extract a rate from rates.

$rates[0]["rate"] = 5
$rates[1]["rate"] = 10
$rates[2]["rate"] = 15
$rates[3]["rate"] = 20
$rates[4]["rate"] = 25

etc

In your current array:

$rates[0]["rate"]  = the rate for animal 0 color 0
$rates[1]["rate"]  = the rate for animal 0 color 1
$rates[2]["rate"]  = the rate for animal 0 color 2
$rates[3]["rate"]  = the rate for animal 1 color 0
$rates[4]["rate"]  = the rate for animal 1 color 1

So to get the rate for animal number $a, color number $c:

$the_rate = $rates[$a*3+$c]["rate"];

The inclusion of the animal and color elements in the inner arrays is 
extra data that isn't needed.

To simplify this even further, we could use exactly the same math to work 
out the index if:

$rates = array (5,10,15,20,25,30,35,40,45,50,55,60);

$the_rate = $rates[$a*3+$c];

In other words, your data at present is being accessed as a 1d array, not 
as a 2d array. The fact that each element of the 1d array is another 1d 
array representing a record, and that you extract the data you want using 
the field name key into the inner array doesn't mean you have a 2d method 
of look up based on animal and color.

Your lookup method is 1d based on animal and color, because animal and 
color combine to make a single key into the outer 1d array.

If you want to use animal ($a) and color ($c) as indexes into a 2d array, 
then you do so like this:

$rates = array(
0 => array ( 5, 10, 15 ),
1 => array ( 20, 25, 30 ),
2 => array ( 35, 40, 45 ),
3 => array ( 50, 55, 60 ) );

Now you have two array keys, $a and $c:

$the_rate = $rates[$a][$c];

Actually, going back to the start of the thread, I suspect that the 
answer to the initial question may have been:

$the_rate = $rates[ $animal_num * 3 + $color_num ]["rate"];

but, and this is /*_critically_*/ important, this only works if there are 
only and exactly three 'color' entries for every animal, and the order of 
the elements in the outer array is fixed with three color entries per 
animal.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#13296

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-30 22:41 -0400
Message-ID<lhakks$l0v$1@dont-email.me>
In reply to#13283
On 03/29/2014 04: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)
> );
>
> $input_animal = 1;
> $input_color = 2;
>
> ?>
>
> How to access the $rates array to echo the associated rate of "animal" => 1 and "color" => 2 ? (which is 30), should i re-designed the $rates array?
>
> Thanks
>

   As another poster has said, a 2D array will work. "animal" and 
"color" are unnecessary as key names since they just need to be passed 
as indexes into the $rates array:

$rates[0][0] = 5;
$rates[0][1] = 10;
$rates[0][2] = 15;
$rates[1][0] = 20;
$rates[1][1] = 25;
$rates[1][2] = 30;

$input_animal = 1;
$input_color = 2;

echo $rates[$input_animal][$input_color]; // = 30

or

$rates["Dog"][0] = 5;
$rates["Dog"][1] = 10;
$rates["Dog"][2] = 15;
$rates["Cat"][0] = 20;
$rates["Cat"][1] = 25;
$rates["Cat"][2] = 30;

$input_animal = "Cat";
$input_color = 1;

echo $rates[$input_animal][$input_color]; // = 25

or

$rates["Dog"]["Black"] = 5;
$rates["Dog"]["White"] = 10;
$rates["Dog"]["Brown"] = 15;
$rates["Cat"]["Black"] = 20;
$rates["Cat"]["White"] = 25;
$rates["Cat"]["Orange"] = 30;

$input_animal = "Dog";
$input_color = "Brown";

echo $rates[$input_animal][$input_color]; // = 15


-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#13297

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-03-31 02:44 +0000
Message-ID<XnsA300E7382EA56dougmilmaccom@78.46.70.116>
In reply to#13283
Kongthap Thammachat <kongthap@gmail.com> wrote in news:f0d2dbc9-bd6e-4407-8d29-
a8d2e6462ebe@googlegroups.com:

> <?php
> 
> $rates = array(
>      array("animal" => 0, "color" => 0, "rate" => 5),
>      array("animal" => 0, "color" => 1, "rate" => 10),
[...]
>      array("animal" => 3, "color" => 1, "rate" => 55),
>      array("animal" => 3, "color" => 2, "rate" => 60)
> );
> 
> $input_animal = 1;
> $input_color = 2;
> 
> ?>
> 
> How to access the $rates array to echo the associated rate of "animal" => 1 and "color" 
=> 2 ? (which is 30), should i re-designed the $rates array?

Rather than redesigning the $rates array, I think you should redesign your entire approach. 
Instead of loading this data into an array in php, load it into a SQL database instead. Then 
your above retrieval is simple:
SELECT rate FROM rates WHERE animal = 1 AND color = 2;

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.php


csiph-web