Array help more than/less than

Is it possible to make an array that can equal something else based on the value being lower or higher.
For example I would need an array so:

input is less than 100 = X
input is more than 100 = Y

but using the array function is that possible?

$array = [
“>100” => “X”,
“<100” => “Y”,
];

Yes… (sorta?)… LOL

I mean your example doesnt really make much sense… what exact are you checking (value?) that is greater or less than ‘100’?


$someValue = 48;
$array = [($someValue > 100 ? 'X' : 'Y'),($someValue > 100 ? 'X' : 'Y')];
var_dump($array);
echo '<br><br>';

$array2 = [(rand(0,200) > 100 ? 'X' : 'Y'),(rand(0,200) > 100 ? 'X' : 'Y')];
var_dump($array2);

How is your array being populated in the first place? (if I’m understanding correctly… that is where probably where you want to alter things)…

$someValue = 100;
$arr = [ < 100 => "X", > 100 => "Y", > 200 => "Z"];

echo $arr[$someValue];

So ill return X or Y or Z based on $someValue from my JSON file.](http://)

Sorry… that doesnt make sense to me.

this:

```
< 100 => "X
```

Is not a complete equation/conditional check.

What is ‘input’? Where does it come from?

You need to be checking SOMETHING against the 100 threshold to see if its greater or lower.

Yeah does $arr[$someValue] not complete the equation as someValue is the input is its what im checking.

NO… and I dont think you are understanding correctly… as this does not really make much sense.

You can create an array and either populate it then…

or you can populate it mater with data.

All this does is:

echo $arr[$someValue];

Is create an array with 1 index, that have a value of 100 in it.

You have done no comparison, no conditional checking…nothing.

This:


< 100 => "X"

does nothing.

Also… if '$someValue is 100… nothing will ever be greater. $someValue (100) > 100? That doesnt make much sense.

By putting this in the array index:

$someValue = 48;
$someValue > 100 ? 'X' : 'Y'

You are checking that $someValue (48 for example) is greater than or less than 100… and output a X or Y based on the results.

(which still doesnt seem very practical, but is valid code, none the less)

Okay I’m like really new so ignore my stupidity.
What would be the best way in creating something like this:

$value = 100; Can be anything from 1 - 100 based on my JSON / API response

if value is more than 20 print X
if value is more than 40 print Y
if value is more than 60 print Z

Is supposed to be for each index? eacdh index in the array should be calculating/checking for EACH of those 3 values? (20, 40, 60…etc)?

Is there any more values to check for?

How many items are supposed to be in the array?

You can alter the initial example I proposed… however… I dont think that keeps it very readable in the end…

SO answer the questions… and I’ll show you an example.

  • How many values/thresholds do you need to check against? (20, 40, 60…more?)
  • How many items/indices are supposed to be in your array?

You only had 2 values in your array example… now many should there be in total?

I have 10 thresholds to check against a single value given back from my API response.
Not sure about the second question, the idea of an array was just what i thought the best way to do it was. The actual thing I’m trying to achieve is check a value against 10 thresholds then output a chosen response on each different threshold if it achieves it.

What should be in the array/index if the value does NOT meet (is greater than) then threshold checked against? Should the index be empty? should have a false value?

here are some other approaches:

$jsonValue = 37;
$resultsArray = [];
switch ($jsonValue) {
    case $jsonValue > 10:
        //add to array
		$resultsArray[] = 'A';
    case $jsonValue > 20:
        //add to array
		$resultsArray[] = 'B';
    case $jsonValue > 30:
        //add to array
		$resultsArray[] = 'C';
	case $jsonValue > 40:
        //add to array
		$resultsArray[] = 'D';
	case $jsonValue > 50:
        //add to array
		$resultsArray[] = 'E';
	case $jsonValue > 60:
        //add to array
		$resultsArray[] = 'F';
	case $jsonValue > 70:
        //add to array
		$resultsArray[] = 'G';	
	case $jsonValue > 80:
        //add to array
		$resultsArray[] = 'H';
	case $jsonValue > 90:
        //add to array
		$resultsArray[] = 'I';
	case $jsonValue > 100:
        //add to array
		$resultsArray[] = 'J';
}

echo '<br><br>';
var_dump($resultsArray);

echo '<br><br>';

$resultsArray2 = [];
for($z=10; $z<=100; $z = $z+10){
	//echo 'Z Check: ' . $z . '<br>';
	if($jsonValue > $z){
		//do something
		$resultsArray2[] = 'Greater Than!';
	}else{
		//do something
		$resultsArray2[] = 'Less Than.';
	}	
}

echo '<br><br>';
var_dump($resultsArray2);

both of these example swill output an array like so:


array(10) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" [3]=> string(1) "D" [4]=> string(1) "E" [5]=> string(1) "F" [6]=> string(1) "G" [7]=> string(1) "H" [8]=> string(1) "I" [9]=> string(1) "J" }



array(10) { [0]=> string(13) "Greater Than!" [1]=> string(13) "Greater Than!" [2]=> string(13) "Greater Than!" [3]=> string(10) "Less Than." [4]=> string(10) "Less Than." [5]=> string(10) "Less Than." [6]=> string(10) "Less Than." [7]=> string(10) "Less Than." [8]=> string(10) "Less Than." [9]=> string(10) "Less Than." } 

OP, What is the real problem you are trying to solve with this code attempt?

Sponsor our Newsletter | Privacy Policy | Terms of Service