Need help accessing the key array. [message #185412] |
Sat, 29 March 2014 20:49 |
Kongthap Thammachat
Messages: 2 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
<?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
|
|
|
Re: Need help accessing the key array. [message #185413 is a reply to message #185412] |
Sat, 29 March 2014 21:05 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185414 is a reply to message #185413] |
Sat, 29 March 2014 22:32 |
Kongthap Thammachat
Messages: 2 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)net
>
> ==================
|
|
|
Re: Need help accessing the key array. [message #185415 is a reply to message #185414] |
Sat, 29 March 2014 23:24 |
Richard Damon
Messages: 58 Registered: August 2011
Karma: 0
|
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)
>>> );
>>>
|
|
|
Re: Need help accessing the key array. [message #185416 is a reply to message #185414] |
Sat, 29 March 2014 23:39 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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
|
|
|
Re: Need help accessing the key array. [message #185417 is a reply to message #185412] |
Sun, 30 March 2014 01:08 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)gmail(dot)com
|
|
|
Re: Need help accessing the key array. [message #185422 is a reply to message #185414] |
Sun, 30 March 2014 15:05 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma: 0
|
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
|
|
|
Re: Need help accessing the key array. [message #185423 is a reply to message #185414] |
Sun, 30 March 2014 17:14 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185424 is a reply to message #185412] |
Sun, 30 March 2014 22:40 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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);
|
|
|
Re: Need help accessing the key array. [message #185425 is a reply to message #185424] |
Sun, 30 March 2014 23:55 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185427 is a reply to message #185412] |
Mon, 31 March 2014 02:41 |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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?-
|
|
|
Re: Need help accessing the key array. [message #185428 is a reply to message #185412] |
Mon, 31 March 2014 02:44 |
Doug Miller
Messages: 171 Registered: August 2011
Karma: 0
|
Senior Member |
|
|
Kongthap Thammachat <kongthap(at)gmail(dot)com> wrote in news:f0d2dbc9-bd6e-4407-8d29-
a8d2e6462ebe(at)googlegroups(dot)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;
|
|
|
Re: Need help accessing the key array. [message #185429 is a reply to message #185425] |
Mon, 31 March 2014 03:15 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)net
>
> ==================
In order for a 2D array to work you have to change the input array and the input parameters. Right?
|
|
|
Re: Need help accessing the key array. [message #185434 is a reply to message #185423] |
Mon, 31 March 2014 05:30 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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
|
|
|
Re: Need help accessing the key array. [message #185445 is a reply to message #185429] |
Mon, 31 March 2014 11:35 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185446 is a reply to message #185434] |
Mon, 31 March 2014 11:37 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185447 is a reply to message #185445] |
Mon, 31 March 2014 13:26 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)net
>
> ==================
But what if you can't change the input array?
|
|
|
Re: Need help accessing the key array. [message #185448 is a reply to message #185447] |
Mon, 31 March 2014 14:33 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185452 is a reply to message #185446] |
Tue, 01 April 2014 00:55 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)gmail(dot)com
|
|
|
Re: Need help accessing the key array. [message #185453 is a reply to message #185452] |
Tue, 01 April 2014 01:09 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185454 is a reply to message #185452] |
Tue, 01 April 2014 02:16 |
Richard Damon
Messages: 58 Registered: August 2011
Karma: 0
|
Member |
|
|
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.
|
|
|
Re: Need help accessing the key array. [message #185461 is a reply to message #185454] |
Tue, 01 April 2014 20:32 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)gmail(dot)com
|
|
|
Re: Need help accessing the key array. [message #185471 is a reply to message #185448] |
Wed, 02 April 2014 13:47 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)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?
|
|
|
Re: Need help accessing the key array. [message #185472 is a reply to message #185471] |
Wed, 02 April 2014 14:51 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185473 is a reply to message #185472] |
Wed, 02 April 2014 16:57 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)net
>
> ==================
|
|
|
Re: Need help accessing the key array. [message #185475 is a reply to message #185471] |
Wed, 02 April 2014 17:21 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)gmail(dot)com
|
|
|
Re: Need help accessing the key array. [message #185476 is a reply to message #185473] |
Wed, 02 April 2014 17:25 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185488 is a reply to message #185476] |
Thu, 03 April 2014 21:20 |
Kevin Burton
Messages: 9 Registered: March 2014
Karma: 0
|
Junior Member |
|
|
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(at)attglobal(dot)net
>
> ==================
|
|
|
Re: Need help accessing the key array. [message #185489 is a reply to message #185488] |
Fri, 04 April 2014 01:11 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Need help accessing the key array. [message #185490 is a reply to message #185473] |
Fri, 04 April 2014 02:13 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)gmail(dot)com
|
|
|