tryingtolearn Posted February 14, 2007 Share Posted February 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 My way of thinking would be to do this: <?php $maxHeight = 0; foreach($array as $key => $add){ $image_height = round($_SESSION['imgheight'.$key]); if($image_height > $maxHeight) { $maxHeight = $image_height; } } ?> Though I'm sure somebody else will have a more efficent way of doing this. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 14, 2007 Author Share Posted February 14, 2007 Thanks for the reply, But that still gives me basically the same thing I had. (Three seperate numbers instead of a string that I can get the max value out of) Maybe Im not explaining it right - Its got me confused. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 14, 2007 Share Posted February 14, 2007 <?php $arr = array(); foreach($array as $key => $add){ $image_height = round($_SESSION['imgheight'.$key]); if($add == "Yes") { $arr[] = $image_height; } } rsort($arr); print_r($arr); ?> Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 14, 2007 Author Share Posted February 14, 2007 Thanks ToonMariner, That did the trick. I appreciate both of you taking the time to respond and help me out. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 15, 2007 Author Share Posted February 15, 2007 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? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 15, 2007 Share Posted February 15, 2007 after rsort... echo $arr[0]; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2007 Share Posted February 15, 2007 After you do the rsort(), the max value is the first value in the array. Ken Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2007 Share Posted February 15, 2007 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; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2007 Share Posted February 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 15, 2007 Author Share Posted February 15, 2007 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2007 Share Posted February 15, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 15, 2007 Author Share Posted February 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 15, 2007 Share Posted February 15, 2007 each tim eyou go through the loop $maxheight gets set again... So first time it is set to 23 then 78 then 416. Look at the logic maxheight only gets reassigned when the current balue is less than image height. Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 15, 2007 Author Share Posted February 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2007 Share Posted February 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
tryingtolearn Posted February 15, 2007 Author Share Posted February 15, 2007 Thanks for the detailed explanations That really does help me understand it alot better. I must have had something set a little different - I am going to go back through and check it out to find why it didnt work. Thanks again - I do appreciate your time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.