Jump to content

[SOLVED] php math error for array


doucettej3

Recommended Posts

i am using a function that finds the smallest value in an array. The array is of random size and has random values. The size is input by the user and the range of the values is input by the user. The output is givin me this :

Array ( [0] => 15 [1] => 3 [2] => 19 [3] => 13 [4] => 9 [5] => 15 [6] => 19 [7] => 6 [8] => 18 [9] => 5 [10] => 5 )

The Smallest number is 1

 

one obviously isnt the smallest number because its not even in the array. Any help with the code would be great. Also it gets the array thru a switch statement with a function that creates the array.

function small_array($passed_array, $random_max, $random_quantity)
{
//initialize the $small variable
$small = $random_max;
for($i = 0; $i < $random_quantity; $i++)
{	if($small >= $passed_array[$i])
{
$small = $passed_array[$i];
}
if($small < $passed_array[$i])
{
$small = $small;
}	
} 
echo "<br>The Smallest number is $small";
}

Link to comment
https://forums.phpfreaks.com/topic/131778-solved-php-math-error-for-array/
Share on other sites

Try:

function small_array($passed_array, $random_max, $random_quantity)
{
//initialize the $small variable
$small = $random_max;
for($i = 0; $i < $random_quantity; $i++)
{	if($small >= $passed_array[$i])
{
$small = $passed_array[$i];
}

} 
echo "<br>The Smallest number is $small";
}

 

for beginning.

 

BTW: you can use min() to get smallest value from an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.