Hi,

I'm trying to create a poll and have an array with 3 values in it. Is there a way to automatically find the largest number in the array?

Thanks.

    hehe, this sounds familiar, we answered this sort of thing somewhere, you probably could search the forums for it

    anyway, you could try using max() to find the highest value in the array.

    conversely you could have a variable that stores the current highest when you are creating the array, but that is messy and the other method can be faster.

      Thanks. Now how do I get the position of that number in the array? For example, if 100 is the highest number in the array, how would I determine that it is in position 2?

        I suspect max() wont help then.
        A possible solution would be to do it yourself, e.g.

        $max_index = 0; //or whatever the first index is
        $max = $array[$max_index];
        foreach ($array as $key => $val) {
        	if ($val > $max) {
        		$max = $val;
        		$max_index = $key;
        	}
        }

          Originally posted by laserlight
          I suspect max() wont help then.
          A possible solution would be to do it yourself, e.g.

          heh, that other thread you're thinking of (and rpanning linked to), contains code to do exactly that.

            Write a Reply...