Ered
Ered

Reputation: 497

php get MIN and MAX from loop results

I have this loop and im wondering how can i get the MIN and MAX value inside the loop:

foreach($result_1 as $key_1) {

if($key_1->ordering > $key_0->ordering ) {
echo $key_1->ordering;
}

}

RESULT : 234

RESULT WANTED IS MIN (2) AND MAX (4) VALUES

Upvotes: 0

Views: 3432

Answers (4)

George
George

Reputation: 280

You just need to loop through the array once and check every value against the current minimum/maximum, and replace it if it is smaller/bigger.

$min = reset( $array )->ordering; // Assign any value to start (just the first in this case)
$max = reset( $array )->ordering; // Assign any value to start (just the first in this case)
foreach ( $array as $object ) {
  //max
  if( $object->ordering > $max ) {
    $max = $object->ordering;
  }

  //min
  if( $object->ordering < $min ) {
    $min = $object->ordering;
  }
}

echo $min;
echo $max;

Upvotes: 2

Ryan
Ryan

Reputation: 14659

You can take the logic of a selection sort and use it to find the minimum value. I'm sure you can figure out from this code how to find the max.

$min = 0;
foreach($result_1 as $key_1) {
   $min = $key_1->ordering
   foreach($result_1 as $key_2) {
     if($min > $key_2->ordering) {
       $min = $key_2->ordering;
     }
   }
}

Here is my test:

$data = array(
    5, 
    6, 
    7, 
    1, 
    9, 
    11, 
    3
);

$min = 0;

foreach($data as $key => $value) {
    $min = $value;
    foreach($data as $key2 => $value2) {
        if($min > $value2) {
            $min = $value2;
        }    
    }
}

echo $min . "\n"; // 1

Upvotes: 1

steinmas
steinmas

Reputation: 398

Just use the min($result_1) and max($result_1) functions that are built into PHP.

https://www.php.net/max https://www.php.net/min

Edit:

Since it's an array of objects, try using two temporary variables to keep track of the min and max. I'm assuming in this code you're looking for the max and min ordering.

$min = 1000000;
$max = -1000000;
foreach($result_1 as $key_1) {
    if($key_1->ordering > $max ) {
       $max = $key_1->ordering;
    }
    else if($key_1->ordering < $min) {
       $min = $key_1->ordering;
    }
}

echo $min;
echo $max;

Upvotes: 1

TimWolla
TimWolla

Reputation: 32711

Sounds like a good job for the functional reduce approach. You can do this in PHP with the array_reduce function:

You pass in an array, a callback and a starting value and the function will call the callback with the current value and the next item from the array and store the result.

php> $array = [ 6, 2, 8, 4 ];
array (
  0 => 6,
  1 => 2,
  2 => 8,
  3 => 4,
)
php> array_reduce($array, 'min', reset($array));
int(2)
php> array_reduce($array, 'max', reset($array));
int(8)

In this example I used min and max respectively as the callback and the first array item as the starting value.

In order to use this properly on your array you can pass in a custom callback using an anonymous function:

function ($a, $b) {
    return max($a->ordering, $b->ordering);
}

Upvotes: 3

Related Questions