FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Need help accessing the key array.
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Need help accessing the key array. [message #185424 is a reply to message #185412] Sun, 30 March 2014 22:40 Go to previous messageGo to previous message
Kevin Burton is currently offline  Kevin Burton
Messages: 9
Registered: March 2014
Karma:
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);
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Most secure way to reset a password via email link
Next Topic: Strange but true! Working with interfaces in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 27 13:59:27 GMT 2024

Total time taken to generate the page: 0.04857 seconds