Jump to content

[SOLVED] find max in a foreach loop


tryingtolearn

Recommended Posts

Hi all

I dont know what this would fall under -

 

I have a foreach loop that is returning the image height for each image selected on the previous page.

I need to get the max() of all the values returned.

 

The problem is that it treats each image height as 1 individual value.

for example

foreach($array as $key => $add){
$image_height = round($_SESSION['imgheight'.$key]);
if($add == "Yes"){
 echo "$image_height<br>";	 
}
} 

Will give me all the image heights selected like this

 

80

275

25

 

Is there a way to take all those individual values returned, combine them, and then return the highest value (275 in this case)?

 

I cant figure out how to get them all together to determine the largest size.

 

 

Thanks for any help.

Link to comment
Share on other sites

Now a new problem.

 

I get the values from the code posted above but I just get the word Array when I try to use it with max()

 

$arr = array();
foreach($array as $key => $add){
$image_height = round($_SESSION['imgheight'.$key]);
if($add == "Yes")
{
  $arr[] = $image_height;
}
}

rsort($arr);

print_r($arr);

 

That gives me

Array ( [0] => 379 [1] => 8 )

for the print_r($arr);

 

But when I try to use the MAX() function with it

I use  max(array($arr));

And it just prints Array instead of the highest value??

 

I then tried putting it into a string

$works = implode(',', $arr);

echo $works;
$hgt = max($works);

 

echo works gives me a comma sep. string of the values

But, I get Wrong parameter count for max when I use the variable.

 

Is there a different way to get the highest value?

Link to comment
Share on other sites

If all you need is the max height and you don't need the other values, that seems like a long way to get where you want to be. I'd suggest this:

 

$maxHeight = 0;
foreach($array as $key => $add){
if($add == "Yes") {
    $image_height = round($_SESSION['imgheight'.$key]);
    if ($image_height>$maxHeight) { $maxHeight = $image_height;}
}
}

echo $maxHeight;

Link to comment
Share on other sites

Another method would be to use the max() function instead of the "if":

<?php
$maxHeight = 0;
foreach($array as $key => $add){
if($add == "Yes") {
    $image_height = round($_SESSION['imgheight'.$key]);
    $maxHeight = max($maxHeight,$image_height);
}
}
echo $maxHeight;
?>

 

Ken

 

 

Link to comment
Share on other sites

Wow - Thats fantastic and confusing.

 

 

All the methods work - and I apologize Balmung-San

You posted the same method as mjdamato did

I wasnt doing something right.

 

With that method - if all the values are greater than 0 what causes the highest # to display?

 

Sorry for being so dense on this, I have tried so many different things I guess Im just not seeing it.

 

Thanks again for all the great help and ideas!

Link to comment
Share on other sites

OK, I'll add comments using kenrbnsn's method which is similar to what I posted, but I like the ingenious use of max():

 

<?php
$maxHeight = 0;
// Loop through each item in the array
foreach($array as $key => $add){

// Check if the value of the item = "Yes"
if($add == "Yes") {

    // If so set $image_height to the value for the image associated with this item
    $image_height = round($_SESSION['imgheight'.$key]);

    // Set $maxHeight equal to the higher value of the current
    // maxHeight value or the current image's height
    $maxHeight = max($maxHeight,$image_height);
}
}
echo $maxHeight;

?>

Link to comment
Share on other sites

Hi and thanks again mjdamato

I see that in kenrbnsn's method with using max

I guess what Im asking is

in the other method -

$maxHeight = 0;
foreach($array as $key => $add){
if($add == "Yes") {
    $image_height = round($_SESSION['imgheight'.$key]);
    if ($image_height>$maxHeight) { $maxHeight = $image_height;}
}
}

if the image heights are 23, 145, 78, 416, 43

 

What sets img_height to 416 in this case since they are all greater than maxHeight

 

Just trying to understand -

THanks

Link to comment
Share on other sites

Yes, that is how I thought it would work - But, what happens is that if you do not have the largest value as the last # it didnt return the largest value.

 

I guess that in the many tests I did where it did work out I just happened to have the largest # last.

 

Using max() works each time.

 

Thanks again.

Link to comment
Share on other sites

It doesn't matter what order the values are in. The "if" statement works the same as using the max() function as the following code illustrates:

 

<?php
$test = array();
for ($i=0;$i<10;$i++)
	$test[$i] = rand(10,500);
echo 'Random heights: ' . implode(', ',$test) . '<br>';
$max_height = 0;
$max_height2 = 0;
foreach ($test as $h) {
	if ($h > $max_height)
		$max_height = $h;
	$max_height2 = max($max_height2,$h);
}
echo 'Maximum via "if" statement = ' . $max_height . '<br>';
echo 'Maximum via "max()" function = ' . $max_height2;
?>

 

Ken

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.