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


Groups > comp.lang.php > #13287

Re: Need help accessing the key array.

From "J.O. Aho" <user@example.net>
Newsgroups comp.lang.php
Subject Re: Need help accessing the key array.
Date 2014-03-30 00:39 +0100
Message-ID <bpp3t4Fe9raU1@mid.individual.net> (permalink)
References <f0d2dbc9-bd6e-4407-8d29-a8d2e6462ebe@googlegroups.com> <lh7cj5$euq$1@dont-email.me> <a54d8fb5-0106-4a07-b161-08da82cb5924@googlegroups.com>

Show all headers | View raw


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

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