2

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!

4 Answers4

1

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;
}
chris85
  • 23,846
  • 7
  • 34
  • 51
  • No I know this max and min already. These are built in functions. However I am looking for some ways how to to do it with just PLAIN FOR LOOPS. I need a bit logic on this one. PLEASE read the problem carefully above mate. –  Jul 04 '15 at 02:55
  • For we are trying our best to use logic instead of just plain PHP built functions. –  Jul 04 '15 at 02:57
  • I suspect using for loops is going to be less efficient than using the min and max functions. – chris85 Jul 04 '15 at 02:59
  • Can max/min function get array keys instead of values? – Hezerac Jul 04 '15 at 03:02
  • There are no keys in the provided array. If you are looking for that this thread should resolve that. http://stackoverflow.com/questions/6126066/search-for-highest-key-index-in-an-array and use the inverse `min` for the minimum. – chris85 Jul 04 '15 at 03:03
  • yeah that's the tricky part there. There should be some ways to add keys or force it to be like this when var_dump($output); //$output should be equal to array('max' => 332, 'min' => 1.02);*/ –  Jul 04 '15 at 03:04
  • can you try to make it a function? –  Jul 04 '15 at 03:13
0
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);
}
bishop
  • 37,830
  • 11
  • 104
  • 139
  • No I know this max and min already. These are built in functions. However I am looking for some ways how to to do it with just PLAIN FOR LOOPS. I need a bit logic on this one. –  Jul 04 '15 at 02:56
  • `max` and `min` will be fastest. `sort` and `array_pop` and `array_unshift` likely next as fast. A `foreach` loop seems tortuous. – bishop Jul 04 '15 at 02:59
  • This foreach loop mate doesnt work. Tried it though. It must be something like this. $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);*/ –  Jul 04 '15 at 03:00
  • Really? Works for me: http://3v4l.org/iFJ1L. Unless you mean the function name is different, in which case, well, I'm not going to do everything for you. – bishop Jul 04 '15 at 03:06
  • I parse the variable instead of the plain fresh array: http://3v4l.org/vJPLD its returning a different thing. –  Jul 04 '15 at 03:13
  • In your example, you're passing the function an array of an array. `$sample = array (...)` then you do `min_and_max(array ($sample))`. Do you really want to pass an array of an array??? – bishop Jul 04 '15 at 03:14
0

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);
Dinuka Dayarathna
  • 169
  • 1
  • 3
  • 9
  • No I know this max and min already. These are built in functions. However I am looking for some ways how to to do it with just PLAIN FOR LOOPS. I need a bit logic on this one. –  Jul 04 '15 at 03:01
0

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;
}
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45