user5072259
user5072259

Reputation:

Create a max and min function with loops - PHP

i am trying to create a function that takes an array of numbers and, then, returns both the minimum and the maximum number from the given array as an associative array using arrays and for loops.

For example:

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02); 
$output = get_max_and_min($sample); 
var_dump($output); 
//$output should be equal to array('max' => 332, 'min' => 1.02);*/

Here's my code so far:

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02); 
    function get_max_and_min($array){
        for($i=0; $i>=$array; $i++){
            echo $i;
        }
    }

    $output = get_max_and_min($sample); 
    echo $output;
  /*  var_dump($output); */

Any idea? Thanks!

Upvotes: 2

Views: 4878

Answers (4)

chandresh_cool
chandresh_cool

Reputation: 11830

If you want logic then you need to loop and compare and store in some temp value :

$max = $temp = 0;
//This loop is to get max value from array
for ($i = 0 ; $i < count($array); $i++) {
   if ($i == 0) {
        $max = $temp = $array[$i];
   }
   if ($i > 0) {
       if ($array[$i] > $temp) {
             $max = $array[$i];

       }
   }
}

echo $max you will get value.

Similarly do for min value like this

$min = $temp = 0;
//this loop is to get min value from array
for ($i = 0 ; $i < count($array); $i++) {
   if ($i == 0) {
        $min = $temp = $array[$i];
   }
   if ($i > 0) {
       if ($array[$i] < $temp) {
             $min = $array[$i];

       }
   }
}

So your complete function is

function get_max_and_min($array) {
     $max = $temp = 0;
      //this loop is to get max value from array
     for ($i = 0 ; $i < count($array); $i++) {
         //check if this is first loop then assign first value as $max
         if ($i == 0) {
             $max = $temp = $array[$i];
         }
        //if this is not first loop then go in this if
        if ($i > 0) {
           //check if this value of array is greater than $temp
           if ($array[$i] > $temp) {
               $max = $array[$i];

           }
        }
    }
     $min = $temp = 0;
      //this loop is to get min value from array
     for ($i = 0 ; $i < count($array); $i++) {
         //check if this is first loop then assign first value as $min
         if ($i == 0) {
             $min = $temp = $array[$i];
         }
         //if this is not first loop then go in this if
         if ($i > 0) {
             //check if this value of array is lesser than $temp
             if ($array[$i] < $temp) {
                 $min = $array[$i];

             }
         }
      }
    $maxMinArray['max'] = $max;
    $maxMinArray['min'] = $min;                
    return $maxMinArray;
}

Upvotes: 0

Dinuka Dayarathna
Dinuka Dayarathna

Reputation: 169

Hope you can get an idea

function getArrayMaxAndMin($sampleArray)
{
    $arrayMax = max($sampleArray);
    $arrayMin = min($sampleArray);
    $result = array('Max'=>$arrayMax,'Min'=>$arrayMin);
    print_r($result);
}

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02);
getArrayMaxAndMin($sample);

Edited answer using for loop

function getArrayMaxAndMin($sampleArray)
{
    for($i=0;$i<count($sampleArray);$i++)
    {
        if($i==0)
        {
            $max = $sampleArray[$i];
            $min = $sampleArray[$i];
        }
        $max = ($max<$sampleArray[$i]?$sampleArray[$i]:$max);
        $min = ($min<$sampleArray[$i]?$min:$sampleArray[$i]);
    }
    $result = array('Max'=>$max,'Min'=>$min);
    print_r($result);
}
$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02);
getArrayMaxAndMin($sample);

Upvotes: 0

chris85
chris85

Reputation: 23880

Demo of how to use min and `max functions.

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02);
echo max($sample) . "\n";
echo min($sample) . "\n";

Output:

332
1.02

Function links:
http://php.net/manual/en/function.max.php
http://php.net/manual/en/function.min.php

Online demo: http://sandbox.onlinephpfunctions.com/code/dd41a06cb470ad6b2a1925d7c017b6b79fb8a880

or as array:

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02);
$values['max'] =  max($sample);
$values['min'] = min($sample);
print_r($values);

Output:

Array
(
    [max] => 332
    [min] => 1.02
)

Update 2, as a function,

$sample = array( 135, 2.4, 2.67, 1.23, 332, 2, 1.02);
print_r(get_min_max($sample));
function get_min_max($array) {    
    $values['max'] =  max($array);
    $values['min'] = min($array);
    return $values;
}

Upvotes: 1

bishop
bishop

Reputation: 39434

function min_and_max(array $array) {
    $min = null;
    $max = null;
    foreach ($array as $number) {
        if ($min === null || $number < $min) {
            $min = $number;
        }
        if ($max === null || $max < $number) {
            $max = $number;
        }
     }
     return array ('max' => $max, 'min' => $min);
}

Upvotes: 0

Related Questions