Using arrays and foreach statements {SOLVED}


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 $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
<?php include('name.php'); ?>
$key1 $get

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


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


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


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

if ($variable >= 333) { $variable = 333; }
echo $variable;

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

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


$maxvalues = array(

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

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).


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.

$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"; } ?>



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

foreach($arkvalues as $var)
if ($total < 1)
$var = floor($var*$total);
echo “