Find min in Array not working?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luftikus143
    New Member
    • Jan 2007
    • 97

    #1

    Find min in Array not working?

    Hi there,

    I have an array, which stores in the first dimension a counter for the number of sets, and in the second the value:

    Code:
    $value[0...240][any_float_possible]
    I found somewhere how to find min and max values in such an array:

    Code:
    	// find max and min value
    	foreach( $value as $__vals )
    	{
    		$__max = max( max($__vals), $__max);
    		$__min = min( min($__vals), $__min);
    	}
    But, logically, the MIN value will be 0 (for positive [any_float_possi ble-values], as it will take the minimum value of the counter; which is 0.

    I can't see what I should do to get the min/max only for the second dimension.

    Thanks for any hints!
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Unfortunately I think you will need to make your own function. You can order an array from smallest to largest, and then loop through the values until you hit one that is not zero. That's just off the top of my head, so there is possibly some better ways to do this that some others might mention.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      I think iterating the array within iteration will do the trick.
      Code:
      $__max = 0;
      $__min = 999999999;
      foreach($values as $__val)
      {
          foreach($__val as $__second_val)
          {
             $__min = min($__second_val, $__min);
             //so is max..
          }
          //similarly for first dimension
          //anything else
      }

      Comment

      Working...