March 18, 200917 yr I'm tring to get the highest number from a foreach loop: $highest = 0; foreach ($numbers->number as $number) { if($number > $highest) { $highest = $number; } } echo $highest; Now why doesn't that work? or this: $i = 0; foreach ($numbers->number as $number) { $numberarray[$i] = $number; $i++; } echo max($numberarray); Surely this is a simple thing to do?
March 18, 200917 yr If $number is an array of numbers, could you not just do: echo max(array_values($numbers)); If $numbers is some other structure, can you give more info?
March 18, 200917 yr Author If $number is an array of numbers, could you not just do: echo max(array_values($numbers)); If $numbers is some other structure, can you give more info? It's a simple XML object.
March 18, 200917 yr Not sure if max() will return what you want as $number is an object. However, using a combo of your first two attempts should work. $highest = 0; foreach($numbers->number as $number) { if($number > $highest) { $highest++; } } echo $highest;
Create an account or sign in to comment