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


Groups > comp.lang.php > #13313

Re: Need help accessing the key array.

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: Need help accessing the key array.
Date 2014-04-02 17:21 +0000
Organization A noiseless patient Spider
Message-ID <lhhgu5$bna$1@dont-email.me> (permalink)
References (3 earlier) <aa753228-e6c4-4501-bf21-e612e18ea7ac@googlegroups.com> <lhbjub$jm5$1@dont-email.me> <c4becf88-13b1-41bc-ab5f-5e55641d6d10@googlegroups.com> <lhbuc6$7ta$1@dont-email.me> <a3c20161-fe6d-4944-93e3-e288f941bd9c@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web