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 #185445 is a reply to message #185429] Mon, 31 March 2014 11:35 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
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
==================
[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: Wed Nov 27 09:16:57 GMT 2024

Total time taken to generate the page: 0.03021 seconds