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


Groups > comp.lang.php > #13298

Re: Need help accessing the key array.

Newsgroups comp.lang.php
Date 2014-03-30 20:15 -0700
References <f0d2dbc9-bd6e-4407-8d29-a8d2e6462ebe@googlegroups.com> <f32ce53f-6454-44b4-ad56-7ca0573c31b6@googlegroups.com> <lhaat8$s0o$1@dont-email.me>
Message-ID <aa753228-e6c4-4501-bf21-e612e18ea7ac@googlegroups.com> (permalink)
Subject Re: Need help accessing the key array.
From Kevin Burton <ronald.kevin.burton@gmail.com>

Show all headers | View raw


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?

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


Thread

Need help accessing the key array. Kongthap Thammachat <kongthap@gmail.com> - 2014-03-29 13:49 -0700
  Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-29 17:05 -0400
    Re: Need help accessing the key array. Kongthap Thammachat <kongthap@gmail.com> - 2014-03-29 15:32 -0700
      Re: Need help accessing the key array. Richard Damon <Richard@Damon-Family.org> - 2014-03-29 19:24 -0400
      Re: Need help accessing the key array. "J.O. Aho" <user@example.net> - 2014-03-30 00:39 +0100
      Re: Need help accessing the key array. Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-30 17:05 +0200
      Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-30 13:14 -0400
        Re: Need help accessing the key array. "J.O. Aho" <user@example.net> - 2014-03-31 07:30 +0200
          Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 07:37 -0400
            Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-01 00:55 +0000
              Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 21:09 -0400
              Re: Need help accessing the key array. Richard Damon <Richard@Damon-Family.org> - 2014-03-31 22:16 -0400
                Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-01 20:32 +0000
  Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-30 01:08 +0000
  Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-30 15:40 -0700
    Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-30 19:55 -0400
      Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-30 20:15 -0700
        Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 07:35 -0400
          Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-03-31 06:26 -0700
            Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-31 10:33 -0400
              Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-02 06:47 -0700
                Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-02 10:51 -0400
                Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-02 09:57 -0700
                Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-02 13:25 -0400
                Re: Need help accessing the key array. Kevin Burton <ronald.kevin.burton@gmail.com> - 2014-04-03 14:20 -0700
                Re: Need help accessing the key array. Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-03 21:11 -0400
                Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-04 02:13 +0000
                Re: Need help accessing the key array. Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-02 17:21 +0000
  Re: Need help accessing the key array. Norman Peelman <npeelman@cfl.rr.com> - 2014-03-30 22:41 -0400
  Re: Need help accessing the key array. Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-31 02:44 +0000

csiph-web