Using arrays and foreach statements {SOLVED}

#1

Ok to explain what is happening “\$total” is an equation of a percentage that im mulitplying with the “\$value” if the total is greater than 100% it makes the number bigger than 333, 300, 166. I don’t want the values to be any bigger than the array values. I know the below code works.

[php]

<?php \$arkonor1 = array("\$mega" => 333, "\$trit" => 300, "\$zyd" => 166); ?> <?php foreach(\$arkonor1 as \$key1 => \$value1) { \$get = floor(\$value1*\$total); echo ""; } ?>
 You Get Gross Station Fee Net \$key1 \$get
[/php]

so i was thinking of adding something like this but was unsure how to do it any suggestions.

[php]

<?php if (\$get >= 333) echo "333"; else echo \$get; ?>

[/php]

my other problem is I need to make the value a variable for other things that i need to do to it. So if the percentage is equal to 97% it take (333*.97) = 323.01 and then make this a variable. Hope i was able to explain it ok to understand. Thank in advance

#2

If you want to use the value in the variable to be no greater than a predetermined value, you could use something like this:

[php]
if (\$variable >= 333) { \$variable = 333; }
echo \$variable;
[/php]

Your last question tells me you’re not a pro in PHP though Once assigned, a variable keeps its value until either:

• unset() is called on the variable
• the variable gets a new value assigned to it
• the script stops running
#3

Thank you for the code it works great for the first number but when the foreach statement loops back to the second value in the statement i need to make that number no highter than 300 and same thing for the 3rd time where that number can’t be higher than 166. any suggestions. Thanks

#4

[php]
\$maxvalues = array(
333,
300,
166
);

for (\$i = 0; \$i < count(\$maxvalues); \$i++) {
if (\$variable >= \$maxvalues[\$i]) { \$variable = \$maxvalues[\$i]; }
}
[/php]

Seriously, this is basic PHP & array knowledge (dare I say, a no-brainer?). You should easily be able to come up with something like this if you can figure out the foreach() loop (or associated arrays for that matter).

#5

Ok not sure what happened or what i miss typed but the only value that is showing up 3 times in a row is 166. Why would this be happening.

[php]
\$arkvalues = array(333, 300, 166);

<?php foreach(\$arkvalues as \$variable) { \$variable1 = floor(\$variable*\$total); for (\$i = 0; \$i < count(\$arkvalues); \$i++) { if (\$variable1 >= \$arkvalues[\$i]) { \$variable1 = \$arkvalues[\$i]; } } echo " \$variable1"; } ?>

[/php]

#6

I was able to solve it with this code but thanks for the help.

[php]
foreach(\$arkvalues as \$var)
{
if (\$total < 1)
\$var = floor(\$var*\$total);
echo “

\$var”;
}
[/php]