Please help me (PHP question)?

casey w

New member
Please correct this for me, I want the for loop to work. Thx

<?php

/* Implement a program that uses a for loop to generate an array of 30 random numbers.
The random numbers are randomly generated integers between 1 and 999. */

$myarray = array(); // assign the variable $myarray
for(count($myarray) < 30 ) { // statement to execute the function to count to 30
array_push($myarray,round(mt_rand(1,999))); // adds the item to the end of the array for $myarray
}
sort($myarray); // sorts the results for $myarray
foreach($myarray as $key => $value){ // incorporate the keys as a value for the associative array for $myarray
echo "Array: $key Value: $value <br>"; // prints the arrays as a key and value as a value
}
echo "The value for the 16th index is $myarray[15]."; // prints the 16th index

echo "<br />Sum of the above arrays is ".array_sum($myarray); // prints the total sum for the values of $myarray

$average = array_sum($myarray)/count($myarray); // calculates the average for values in a $average variable

echo "<br />The average for the above array is ".$average; // prints the average for the $average variable

$shortval = sprintf("%01.2f",$average); // returns a formatted string as $shortval

echo "<br />The largest item in the array is ".array_pop($myarray);echo"."; // prints the removed item from the end of the $myarray array

echo "<br />The smallest item in the array is ".array_shift($myarray);echo". <br />"; // prints the removed item from the beginning of the $myarray array

while($value = array_shift($myarray)){ // statement to execute the removal of the item from the beginning of the $myarray array

echo "The array for ".count($myarray); // prints the remaining $myarray key

echo ", has a value of ".$value."<br>"; // prints the corresponding value in the element

}
exit();
?>
 
Back
Top