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 20 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 1 of 2  [1] 2  Next page →


#13283 — Need help accessing the key array.

FromKongthap Thammachat <kongthap@gmail.com>
Date2014-03-29 13:49 -0700
SubjectNeed help accessing the key array.
Message-ID<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" => 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

[toc] | [next] | [standalone]


#13284

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-29 17:05 -0400
Message-ID<lh7cj5$euq$1@dont-email.me>
In reply to#13283
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)
> );
>
> $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
>

$rates is an array from [0]..[n].  So in this case it would be $rates[5].

Part of the problem is the way you're doing this is very hard to 
understand.  But not knowing what you're trying to do, I can't give you 
any better recommendations.

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

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


#13285

FromKongthap Thammachat <kongthap@gmail.com>
Date2014-03-29 15:32 -0700
Message-ID<a54d8fb5-0106-4a07-b161-08da82cb5924@googlegroups.com>
In reply to#13284
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)
> 
> > );
> 
> >
> 
> > $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
> 
> >
> 
> 
> 
> $rates is an array from [0]..[n].  So in this case it would be $rates[5].
> 
> 
> 
> Part of the problem is the way you're doing this is very hard to 
> 
> understand.  But not knowing what you're trying to do, I can't give you 
> 
> any better recommendations.
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

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


#13286

FromRichard Damon <Richard@Damon-Family.org>
Date2014-03-29 19:24 -0400
Message-ID<bDIZu.20718$ZB7.10661@en-nntp-16.dc1.easynews.com>
In reply to#13285
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)
>>> );
>>>

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


#13287

From"J.O. Aho" <user@example.net>
Date2014-03-30 00:39 +0100
Message-ID<bpp3t4Fe9raU1@mid.individual.net>
In reply to#13285
On 29/03/14 23:32, Kongthap Thammachat wrote:

First a couple of tips:
1. Don't top post, if you reply to something someone else has written, 
then type that after the question.

2. If you don't replay to a part of the previous message, delete it, for 
example mail footers.

4. Try to use less than 80 character long lines, makes things more 
readable for others.

3. Use a proper nntp service, which don't add extra line breaks to messages.


> 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?

You could use array_filter()
http://www.php.net/manual/en/function.array-filter.php

<?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;

function findRate($array) {
         global $input_animal, $input_color;
         return $array['animal'] == $input_animal && $array['color'] == 
$input_color;
}

print_r(array_filter($rates, "findRate"));
?>

This will work like if you had looped trough the array, and you will 
check each cell in the array (if you are looking for the first 
occurrence and using a normal loop, you can always break the loop and 
those you do not have to loop trough the whole array.

Maybe you should rethink how to setup the array, having something like

$rates = array(
  0 => array( 0 => 5),
  0 => array( 1 => 10),
  ...
  3 => array( 2 => 60)
);

this way you have to check that there is a cell in the multi dimensional 
array and then take out the value as $rates[1][2]


-- 

  //Aho

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


#13292

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-30 17:05 +0200
Message-ID<533832b7$0$22620$9b4e6d93@newsspool1.arcor-online.net>
In reply to#13285
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

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


#13293

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-30 13:14 -0400
Message-ID<lh9jdh$5ii$1@dont-email.me>
In reply to#13285
On 3/29/2014 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.
>

Please do not top post.  It makes the sequence very hard to read.

I agree with Denis and J.O. - they have a much better way of handling 
this.  But some other comments:

1. Why use 0, 1 and 2?  Why not "bear", "tiger" and dog" for the animal, 
and "black", "brown" and "white" for the color (as an example)?  This 
will be much easier to understand.  And the HTML, if correctly defined, 
can return these as easily as 0,1 and 2.

Additionally - if this is all you will ever have, then what you have may 
work fine.  The main question I would have is - if you have to change 
the rate, add a color, or whatever - how much code are you going to have 
to change?

If you ever are going to have something different, my suggestion would 
be to start using a database such as MySQL or PostgreSQL now.  Using a 
database means that all you have to do is update the database for new 
items or attributes or a rate change, and the results will be reflected 
in all of your pages.  The database will also handle the searches for you.

It takes a bit more work up front, but will save in the long run.  It 
will also be easier to start using a database now then to try to change 
your code later.

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

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


#13299

From"J.O. Aho" <user@example.net>
Date2014-03-31 07:30 +0200
Message-ID<bpscshF4k15U1@mid.individual.net>
In reply to#13293
On 30/03/14 19:14, Jerry Stuckle wrote:

> If you ever are going to have something different, my suggestion would
> be to start using a database such as MySQL or PostgreSQL now.  Using a
> database means that all you have to do is update the database for new
> items or attributes or a rate change, and the results will be reflected
> in all of your pages.  The database will also handle the searches for you.

Just a couple of inputs.

Don't forget that PostgeSQL has support for arrays and json objects, 
which can be useful in some cases. (PostgrSQL can be find at a fair 
number of web hosting companies).

Also I should mention neo4j, a good graph-database, if you need 
something which is fast for finding nested relations, there is an 
extension for PHP. (of course you will not find this at web hosting 
companies, you need to have your own VM)

-- 

  //Aho

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


#13301

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-31 07:37 -0400
Message-ID<lhbk1d$jm5$2@dont-email.me>
In reply to#13299
On 3/31/2014 1:30 AM, J.O. Aho wrote:
> On 30/03/14 19:14, Jerry Stuckle wrote:
>
>> If you ever are going to have something different, my suggestion would
>> be to start using a database such as MySQL or PostgreSQL now.  Using a
>> database means that all you have to do is update the database for new
>> items or attributes or a rate change, and the results will be reflected
>> in all of your pages.  The database will also handle the searches for
>> you.
>
> Just a couple of inputs.
>
> Don't forget that PostgeSQL has support for arrays and json objects,
> which can be useful in some cases. (PostgrSQL can be find at a fair
> number of web hosting companies).
>
> Also I should mention neo4j, a good graph-database, if you need
> something which is fast for finding nested relations, there is an
> extension for PHP. (of course you will not find this at web hosting
> companies, you need to have your own VM)
>

I see no need for arrays or json objects in a database here.  Or a graph 
database, for that matter.

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

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


#13305

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-04-01 00:55 +0000
Message-ID<lhd2qk$m38$1@dont-email.me>
In reply to#13301
On Mon, 31 Mar 2014 07:37:19 -0400, Jerry Stuckle wrote:

> I see no need for arrays or json objects in a database here.  Or a graph
> database, for that matter.

Agreed. A table with three elements per row, namely animal, colour, rate, 
keyed on the combination of animal and colour.

Or possible 3 tables:

animals: animal_name* | animal_id

colours: colour* | colour_id

rates: animal_id* | colour_id* | rate

Where * indicates the field(s) that make the PK for the table, but I 
suspect that's overkill for the task as described so far.

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#13306

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-31 21:09 -0400
Message-ID<lhd3k8$c5k$1@dont-email.me>
In reply to#13305
On 3/31/2014 8:55 PM, Denis McMahon wrote:
> On Mon, 31 Mar 2014 07:37:19 -0400, Jerry Stuckle wrote:
>
>> I see no need for arrays or json objects in a database here.  Or a graph
>> database, for that matter.
>
> Agreed. A table with three elements per row, namely animal, colour, rate,
> keyed on the combination of animal and colour.
>
> Or possible 3 tables:
>
> animals: animal_name* | animal_id
>
> colours: colour* | colour_id
>
> rates: animal_id* | colour_id* | rate
>
> Where * indicates the field(s) that make the PK for the table, but I
> suspect that's overkill for the task as described so far.
>

Looks like a good design to me, Denis. :)

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

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


#13307

FromRichard Damon <Richard@Damon-Family.org>
Date2014-03-31 22:16 -0400
Message-ID<ykp_u.31776$Wy2.29581@en-nntp-16.dc1.easynews.com>
In reply to#13305
On 3/31/14, 8:55 PM, Denis McMahon wrote:
> On Mon, 31 Mar 2014 07:37:19 -0400, Jerry Stuckle wrote:
> 
>> I see no need for arrays or json objects in a database here.  Or a graph
>> database, for that matter.
> 
> Agreed. A table with three elements per row, namely animal, colour, rate, 
> keyed on the combination of animal and colour.
> 
> Or possible 3 tables:
> 
> animals: animal_name* | animal_id
> 
> colours: colour* | colour_id
> 
> rates: animal_id* | colour_id* | rate
> 
> Where * indicates the field(s) that make the PK for the table, but I 
> suspect that's overkill for the task as described so far.
> 

I would probably make animal_id and colour_id the PK for those tables,
as that is what other tables are going to refer to via Foreign Keys.
animal_name and colour would be indexed, and possible unique.

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


#13309

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-04-01 20:32 +0000
Message-ID<lhf7o9$aep$1@dont-email.me>
In reply to#13307
On Mon, 31 Mar 2014 22:16:58 -0400, Richard Damon wrote:

> On 3/31/14, 8:55 PM, Denis McMahon wrote:
>> On Mon, 31 Mar 2014 07:37:19 -0400, Jerry Stuckle wrote:
>> 
>>> I see no need for arrays or json objects in a database here.  Or a
>>> graph database, for that matter.
>> 
>> Agreed. A table with three elements per row, namely animal, colour,
>> rate,
>> keyed on the combination of animal and colour.
>> 
>> Or possible 3 tables:
>> 
>> animals: animal_name* | animal_id
>> 
>> colours: colour* | colour_id
>> 
>> rates: animal_id* | colour_id* | rate
>> 
>> Where * indicates the field(s) that make the PK for the table, but I
>> suspect that's overkill for the task as described so far.

> I would probably make animal_id and colour_id the PK for those tables,
> as that is what other tables are going to refer to via Foreign Keys.
> animal_name and colour would be indexed, and possible unique.

I wouldn't include them at all given the scope of the problem as defined, 
because they're clearly not needed. They may never become needed, if 
"animal" is a unique identifier then there seems to be little benefit in 
having a table simply to map a 1:1 relationship animal : animal_id such 
that animal_id is used in many places.

The same goes for colour.

There may be a benefit in a large project in replacing strings with 
integers in terms of memory and storage requirements, but in the scenario 
described with 9 possible rates the overhead of the additional tables is 
likely to cancel out the benefit.

It's also possible that on a 64 bit system, unless a high proportion of 
both animals and colours when expressed as strings exceed 8 characters in 
length, savings vary between marginal and none.

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#13288

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-30 01:08 +0000
Message-ID<lh7qr7$4hi$1@dont-email.me>
In reply to#13283
On Sat, 29 Mar 2014 13:49:37 -0700, 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

If you're trying to find the rate for the input animal and colour values, 
you're going to have to walk through all the members of rates, comparing 
their animal and colour elements with the input values:

foreach ( $rates as $key => $value ) {
if ( $value["animal"] == $input_animal && $value["color"] == "$input_color 
) {
break;
} }

echo "key into rates is: {$key}\n";
echo "rate = {$rates[$key]["rate"]}\n";
echo "rate also = {$value["rate"]}\n";

An alternative data structure might be:

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

echo "rate = = {$rates[$input_animal][$input_color]}\n";

But whether that is better or not that will depend on what you're 
actually trying to do.

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#13294

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-03-30 15:40 -0700
Message-ID<f32ce53f-6454-44b4-ad56-7ca0573c31b6@googlegroups.com>
In reply to#13283
On Saturday, March 29, 2014 3:49:37 PM UTC-5, 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

I don't think there is a way to reference the array as I understand you want to (at least I don't know of a way). But perhaps the following classes will give you the results that you desire:

class FilterClass {
    private $input;
    private $filter;
    function __construct($filter, $input) {
        $this->filter = $filter;
        $this->input = $input;
    }
    function FilteredArray() {
        return array_filter($this->input, function($val) {
            $matched = false;
            foreach(array_keys($this->filter) as $filter_key) {
                if($val[$filter_key] == $this->filter[$filter_key]) {
                    $matched = true;
                } else {
                    $matched = false;
                    break;
                }
            }
            return $matched;
        });
    }
};

class ReduceClass {
    private $input;
    private $filter;
    function __construct($filter, $input) {
        $this->filter = $filter;
        $this->input = $input;
    }
    function ReducedArray() {
        return array_reduce($this->input, function($acc, $val) {
            $matched = false;
            foreach(array_keys($this->filter) as $filter_key) {
                if($val[$filter_key] == $this->filter[$filter_key]) {
                    $matched = true;
                } else {
                    $matched = false;
                    break;
                }
            }
            if($matched) {
                array_push($acc, $val);
            }
            return $acc;
        }, array());
    }
};


        $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)
        );
        $fc = new FilterClass(array("animal" => 1, "color" => 2), $rates);
        $filtered_array = $fc->FilteredArray();
        var_dump($filtered_array);

Or

        $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)
        );
        $fc = new ReduceClass(array("animal" => 1, "color" => 2), $rates);
        $filtered_array = $fc->ReducedArray();
        var_dump($filtered_array);

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


#13295

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-30 19:55 -0400
Message-ID<lhaat8$s0o$1@dont-email.me>
In reply to#13294
On 3/30/2014 6:40 PM, Kevin Burton wrote:
> On Saturday, March 29, 2014 3:49:37 PM UTC-5, 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
>
> I don't think there is a way to reference the array as I understand you want to (at least I don't know of a way). But perhaps the following classes will give you the results that you desire:
>
> class FilterClass {
>      private $input;
>      private $filter;
>      function __construct($filter, $input) {
>          $this->filter = $filter;
>          $this->input = $input;
>      }
>      function FilteredArray() {
>          return array_filter($this->input, function($val) {
>              $matched = false;
>              foreach(array_keys($this->filter) as $filter_key) {
>                  if($val[$filter_key] == $this->filter[$filter_key]) {
>                      $matched = true;
>                  } else {
>                      $matched = false;
>                      break;
>                  }
>              }
>              return $matched;
>          });
>      }
> };
>
> class ReduceClass {
>      private $input;
>      private $filter;
>      function __construct($filter, $input) {
>          $this->filter = $filter;
>          $this->input = $input;
>      }
>      function ReducedArray() {
>          return array_reduce($this->input, function($acc, $val) {
>              $matched = false;
>              foreach(array_keys($this->filter) as $filter_key) {
>                  if($val[$filter_key] == $this->filter[$filter_key]) {
>                      $matched = true;
>                  } else {
>                      $matched = false;
>                      break;
>                  }
>              }
>              if($matched) {
>                  array_push($acc, $val);
>              }
>              return $acc;
>          }, array());
>      }
> };
>
>
>          $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)
>          );
>          $fc = new FilterClass(array("animal" => 1, "color" => 2), $rates);
>          $filtered_array = $fc->FilteredArray();
>          var_dump($filtered_array);
>
> Or
>
>          $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)
>          );
>          $fc = new ReduceClass(array("animal" => 1, "color" => 2), $rates);
>          $filtered_array = $fc->ReducedArray();
>          var_dump($filtered_array);
>

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.

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

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


#13298

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-03-30 20:15 -0700
Message-ID<aa753228-e6c4-4501-bf21-e612e18ea7ac@googlegroups.com>
In reply to#13295
On Sunday, March 30, 2014 6:55:22 PM UTC-5, Jerry Stuckle wrote:
> On 3/30/2014 6:40 PM, Kevin Burton wrote:
> 
> > On Saturday, March 29, 2014 3:49:37 PM UTC-5, 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
> 
> >
> 
> > I don't think there is a way to reference the array as I understand you want to (at least I don't know of a way). But perhaps the following classes will give you the results that you desire:
> 
> >
> 
> > class FilterClass {
> 
> >      private $input;
> 
> >      private $filter;
> 
> >      function __construct($filter, $input) {
> 
> >          $this->filter = $filter;
> 
> >          $this->input = $input;
> 
> >      }
> 
> >      function FilteredArray() {
> 
> >          return array_filter($this->input, function($val) {
> 
> >              $matched = false;
> 
> >              foreach(array_keys($this->filter) as $filter_key) {
> 
> >                  if($val[$filter_key] == $this->filter[$filter_key]) {
> 
> >                      $matched = true;
> 
> >                  } else {
> 
> >                      $matched = false;
> 
> >                      break;
> 
> >                  }
> 
> >              }
> 
> >              return $matched;
> 
> >          });
> 
> >      }
> 
> > };
> 
> >
> 
> > class ReduceClass {
> 
> >      private $input;
> 
> >      private $filter;
> 
> >      function __construct($filter, $input) {
> 
> >          $this->filter = $filter;
> 
> >          $this->input = $input;
> 
> >      }
> 
> >      function ReducedArray() {
> 
> >          return array_reduce($this->input, function($acc, $val) {
> 
> >              $matched = false;
> 
> >              foreach(array_keys($this->filter) as $filter_key) {
> 
> >                  if($val[$filter_key] == $this->filter[$filter_key]) {
> 
> >                      $matched = true;
> 
> >                  } else {
> 
> >                      $matched = false;
> 
> >                      break;
> 
> >                  }
> 
> >              }
> 
> >              if($matched) {
> 
> >                  array_push($acc, $val);
> 
> >              }
> 
> >              return $acc;
> 
> >          }, array());
> 
> >      }
> 
> > };
> 
> >
> 
> >
> 
> >          $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)
> 
> >          );
> 
> >          $fc = new FilterClass(array("animal" => 1, "color" => 2), $rates);
> 
> >          $filtered_array = $fc->FilteredArray();
> 
> >          var_dump($filtered_array);
> 
> >
> 
> > Or
> 
> >
> 
> >          $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)
> 
> >          );
> 
> >          $fc = new ReduceClass(array("animal" => 1, "color" => 2), $rates);
> 
> >          $filtered_array = $fc->ReducedArray();
> 
> >          var_dump($filtered_array);
> 
> >
> 
> 
> 
> 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.
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

In order for a 2D array to work you have to change the input array and the input parameters. Right?

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


#13300

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-31 07:35 -0400
Message-ID<lhbjub$jm5$1@dont-email.me>
In reply to#13298
On 3/30/2014 11:15 PM, Kevin Burton wrote:
> On Sunday, March 30, 2014 6:55:22 PM UTC-5, Jerry Stuckle wrote:
>> On 3/30/2014 6:40 PM, Kevin Burton wrote:
>>
>>> On Saturday, March 29, 2014 3:49:37 PM UTC-5, 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
>>
>>>
>>
>>> I don't think there is a way to reference the array as I understand you want to (at least I don't know of a way). But perhaps the following classes will give you the results that you desire:
>>
>>>
>>
>>> class FilterClass {
>>
>>>       private $input;
>>
>>>       private $filter;
>>
>>>       function __construct($filter, $input) {
>>
>>>           $this->filter = $filter;
>>
>>>           $this->input = $input;
>>
>>>       }
>>
>>>       function FilteredArray() {
>>
>>>           return array_filter($this->input, function($val) {
>>
>>>               $matched = false;
>>
>>>               foreach(array_keys($this->filter) as $filter_key) {
>>
>>>                   if($val[$filter_key] == $this->filter[$filter_key]) {
>>
>>>                       $matched = true;
>>
>>>                   } else {
>>
>>>                       $matched = false;
>>
>>>                       break;
>>
>>>                   }
>>
>>>               }
>>
>>>               return $matched;
>>
>>>           });
>>
>>>       }
>>
>>> };
>>
>>>
>>
>>> class ReduceClass {
>>
>>>       private $input;
>>
>>>       private $filter;
>>
>>>       function __construct($filter, $input) {
>>
>>>           $this->filter = $filter;
>>
>>>           $this->input = $input;
>>
>>>       }
>>
>>>       function ReducedArray() {
>>
>>>           return array_reduce($this->input, function($acc, $val) {
>>
>>>               $matched = false;
>>
>>>               foreach(array_keys($this->filter) as $filter_key) {
>>
>>>                   if($val[$filter_key] == $this->filter[$filter_key]) {
>>
>>>                       $matched = true;
>>
>>>                   } else {
>>
>>>                       $matched = false;
>>
>>>                       break;
>>
>>>                   }
>>
>>>               }
>>
>>>               if($matched) {
>>
>>>                   array_push($acc, $val);
>>
>>>               }
>>
>>>               return $acc;
>>
>>>           }, array());
>>
>>>       }
>>
>>> };
>>
>>>
>>
>>>
>>
>>>           $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)
>>
>>>           );
>>
>>>           $fc = new FilterClass(array("animal" => 1, "color" => 2), $rates);
>>
>>>           $filtered_array = $fc->FilteredArray();
>>
>>>           var_dump($filtered_array);
>>
>>>
>>
>>> Or
>>
>>>
>>
>>>           $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)
>>
>>>           );
>>
>>>           $fc = new ReduceClass(array("animal" => 1, "color" => 2), $rates);
>>
>>>           $filtered_array = $fc->ReducedArray();
>>
>>>           var_dump($filtered_array);
>>
>>>
>>
>>
>>
>> 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).

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

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


#13302

FromKevin Burton <ronald.kevin.burton@gmail.com>
Date2014-03-31 06:26 -0700
Message-ID<c4becf88-13b1-41bc-ab5f-5e55641d6d10@googlegroups.com>
In reply to#13300
On Monday, March 31, 2014 6:35:41 AM UTC-5, Jerry Stuckle wrote:
> On 3/30/2014 11:15 PM, Kevin Burton wrote:
> 
> > On Sunday, March 30, 2014 6:55:22 PM UTC-5, Jerry Stuckle wrote:
> 
> >> On 3/30/2014 6:40 PM, Kevin Burton wrote:
> 
> >>
> 
> >>> On Saturday, March 29, 2014 3:49:37 PM UTC-5, 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
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> I don't think there is a way to reference the array as I understand you want to (at least I don't know of a way). But perhaps the following classes will give you the results that you desire:
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> class FilterClass {
> 
> >>
> 
> >>>       private $input;
> 
> >>
> 
> >>>       private $filter;
> 
> >>
> 
> >>>       function __construct($filter, $input) {
> 
> >>
> 
> >>>           $this->filter = $filter;
> 
> >>
> 
> >>>           $this->input = $input;
> 
> >>
> 
> >>>       }
> 
> >>
> 
> >>>       function FilteredArray() {
> 
> >>
> 
> >>>           return array_filter($this->input, function($val) {
> 
> >>
> 
> >>>               $matched = false;
> 
> >>
> 
> >>>               foreach(array_keys($this->filter) as $filter_key) {
> 
> >>
> 
> >>>                   if($val[$filter_key] == $this->filter[$filter_key]) {
> 
> >>
> 
> >>>                       $matched = true;
> 
> >>
> 
> >>>                   } else {
> 
> >>
> 
> >>>                       $matched = false;
> 
> >>
> 
> >>>                       break;
> 
> >>
> 
> >>>                   }
> 
> >>
> 
> >>>               }
> 
> >>
> 
> >>>               return $matched;
> 
> >>
> 
> >>>           });
> 
> >>
> 
> >>>       }
> 
> >>
> 
> >>> };
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> class ReduceClass {
> 
> >>
> 
> >>>       private $input;
> 
> >>
> 
> >>>       private $filter;
> 
> >>
> 
> >>>       function __construct($filter, $input) {
> 
> >>
> 
> >>>           $this->filter = $filter;
> 
> >>
> 
> >>>           $this->input = $input;
> 
> >>
> 
> >>>       }
> 
> >>
> 
> >>>       function ReducedArray() {
> 
> >>
> 
> >>>           return array_reduce($this->input, function($acc, $val) {
> 
> >>
> 
> >>>               $matched = false;
> 
> >>
> 
> >>>               foreach(array_keys($this->filter) as $filter_key) {
> 
> >>
> 
> >>>                   if($val[$filter_key] == $this->filter[$filter_key]) {
> 
> >>
> 
> >>>                       $matched = true;
> 
> >>
> 
> >>>                   } else {
> 
> >>
> 
> >>>                       $matched = false;
> 
> >>
> 
> >>>                       break;
> 
> >>
> 
> >>>                   }
> 
> >>
> 
> >>>               }
> 
> >>
> 
> >>>               if($matched) {
> 
> >>
> 
> >>>                   array_push($acc, $val);
> 
> >>
> 
> >>>               }
> 
> >>
> 
> >>>               return $acc;
> 
> >>
> 
> >>>           }, array());
> 
> >>
> 
> >>>       }
> 
> >>
> 
> >>> };
> 
> >>
> 
> >>>
> 
> >>
> 
> >>>
> 
> >>
> 
> >>>           $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)
> 
> >>
> 
> >>>           );
> 
> >>
> 
> >>>           $fc = new FilterClass(array("animal" => 1, "color" => 2), $rates);
> 
> >>
> 
> >>>           $filtered_array = $fc->FilteredArray();
> 
> >>
> 
> >>>           var_dump($filtered_array);
> 
> >>
> 
> >>>
> 
> >>
> 
> >>> Or
> 
> >>
> 
> >>>
> 
> >>
> 
> >>>           $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)
> 
> >>
> 
> >>>           );
> 
> >>
> 
> >>>           $fc = new ReduceClass(array("animal" => 1, "color" => 2), $rates);
> 
> >>
> 
> >>>           $filtered_array = $fc->ReducedArray();
> 
> >>
> 
> >>>           var_dump($filtered_array);
> 
> >>
> 
> >>>
> 
> >>
> 
> >>
> 
> >>
> 
> >> 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).
> 
> 
> 
> -- 
> 
> ==================
> 
> Remove the "x" from my email address
> 
> Jerry Stuckle
> 
> jstucklex@attglobal.net
> 
> ==================

But what if you can't change the input array?

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


#13303

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-31 10:33 -0400
Message-ID<lhbuc6$7ta$1@dont-email.me>
In reply to#13302
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
==================

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web