Friday, August 28, 2015

How to get min number from an array

/*
 * How to get min number from a array
 * */
$numbers = array(9, 0, 1, 77, 4, 5, -1, 10);

$min_number = $numbers[0];
$length = count($numbers);//in order to save time we initial length outside of the loops
for($i = 0; $i < $length; $i++){
   if ($min_number > $numbers[$i]){
       $min_number = $numbers[$i];
   }
}
echo "Min number is the array is ", $min_number;

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }