0

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

5
  • What does $result_1 look like? (Show us what var_dump($result_1) outputs.) Commented Feb 13, 2014 at 1:05
  • if i use echo max($key_1->ordering); i get Warning: max() [function.max]: When only one parameter is given, it must be an array in Commented Feb 13, 2014 at 1:06
  • 1
    Actually I delete my first comment based on revision. Can you please explain what $key_1 and $key_0 are and what these max to do with max and min values? Should you not jsut be putting the value results from your comparison into an array and then performing max() and min() on that array? Clearly if you want to analyze a set of values like this, you need to store them in a structure that allows you to do so rather then print every single case where your criteria is met to screen. Commented Feb 13, 2014 at 1:09
  • @Ered We need to see what is in $result_1. Commented Feb 13, 2014 at 1:10
  • @SverriM.Olsen the output dump is to big to post Commented Feb 13, 2014 at 1:14

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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;

4 Comments

anyway i can get the MIN and MAX inside the loop?
@Ered You won't know the minimum and maximum until after the loop has finished, as it won't have had a chance to check all of the values.
@jeroen This is true, thanks. A better way would be to use the inbuilt first() function. Answer edited to reflect this.
@jeroen Oops I did, my bad. Should really check manual before posting.
1

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;

4 Comments

I edited my answer with an alternative method. Thanks for pointing this out Sverri!
To be safe, you could use reset($result_1)->ordering as your initial values.
You shouldn't guess the initial $min and $max values. You should use a value which is in the array in question. In the case of min/max it can be any value, as every value gets compared anyway. See my answer
Also shouldn't be an elseif, as a value can be both the minimum and maximum in each iteration of the loop.
1

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

5 Comments

You missed a > in your conditional statement.
@jeroen I don't think so, I've just tested it. Will post script here.
@jeroen Have you tested your conclusion? I've tested it on multiple sets. Take a look at my test run.
You're right, I wasn't paying attention. Seems a bit much though, the double loop when you can just set the initial value to the first value (any value...) of the input array.
@jeroen - What you're describing is another (maybe more efficient) was of doing the selection sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.