0

Hey all i am new at classes and arrays in php but need to find out how to go about getting the correct value from this class function array

class theCart {
    public static $DISTANCE = array(
      '0'  => '0 - 75',
      '10' => '76 - 125',
      '20' => '126 - 175',
      '30' => '176 - 225',
      '40' => '226 - 275',
      '50' => '276 - 325'
    );
}

My output i am trying to match looks like this: 76 - 125

Do i just call it like

 $distanceNum = '76 - 125';
 $tmpDistanceTotal = $DISTANCE($distanceNum);

Should $tmpDistanceTotal then have a value of 10? I'm thinking that the array only has the values 0,10,20,30,40,50 in it?

I have another array:

public static $STEPS = array(
    '0' => 0,
    '1' => 0,
    '2' => 0,
    '3' => 25,
    '4' => 50,
    '5' => 75,
    '6' => 100,
    '7' => 125
);

My output i am trying to match with that above is 3 I'm not sure if its looking for a string or not?

4 Answers 4

3

This should clear the point:

foreach (theCart::$DISTANCE as $k => $v) {
    if ($v == '76 - 125') {
        echo $k;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

For additional clarity, use the scope resolution operator i.e :: to access static class variables like the code above. You can read more about it here php.net/manual/en/language.oop5.static.php
Thanks for the code! However, it does not seem to work for my second array that i have? Check out my uploaded OP.
2

For $tmpDistanceTotal to get the value 10, you could do the following:

$tmpDistanceTotal = array_search($distanceNum, theCart::DISTANCE);

or you may wish to end up with something like this:

class theCart {
    public static $DISTANCE = array(
      '0'  => '0 - 75',
      '10' => '76 - 125',
      '20' => '126 - 175',
      '30' => '176 - 225',
      '40' => '226 - 275',
      '50' => '276 - 325'
    );

    public function getTotalDistance($distanceNum)
    {
         return array_search($distanceNum, self::DISTANCE);
    }
}

3 Comments

Thanks for the code! However, it does not seem to work for my second array that i have? Check out my uploaded OP.
I seem to be looking for a value form the left side of the array in my second array in the OP instead of the right with my first array example in my OP. I tried using $key = array_search($steps, theCart::$STEPS); but that did not work.
I am not sure what $steps is in your example here. Seems that $steps should be a string though to match against the strings for keys in theCart::STEPS
1

Your question is actually just about arrays, and you should remove the classes from here to make things easier to understand:

$DISTANCE = array(
  '0'  => '0 - 75',
  '10' => '76 - 125',
  '20' => '126 - 175',
  '30' => '176 - 225',
  '40' => '226 - 275',
  '50' => '276 - 325'
);

$variable = $DISTANCE[10];

In the above example Variable will be equal to 76-125. You're working with Associative Arrays, so you need to go read up on them a little bit as your questions shows you don't really understand how arrays work. Once you have that down, go ahead and move into a class context like you mentioned above.

You can check out the PHP Manual here: http://php.net/manual/en/language.types.array.php

For a short and quick answer you can use

 $tempVar = 10;
 $tmpDistance = $this->DISTANCE[$tempVar];

1 Comment

As someone above mentioned, since it's a static variable this wont quite work, but I think you need to understand how to access class variables and how an array works first.
1

Not sure what you are trying to do, but you could use array_search:

$distanceNum = '76 - 125';
$key = array_search($distanceNum, theCart::$DISTANCE);

$key is now 10.

2 Comments

Thanks for the code! However, it does not seem to work for my second array that i have? Check out my uploaded OP.
I seem to be looking for a value form the left side of the array in my second array in the OP instead of the right with my first array example in my OP. I tried using $key = array_search($steps, theCart::$STEPS); but that did not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.