How to use max() for 2-dimensional array?

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

    How to use max() for 2-dimensional array?

    Hello,

    I have an array which is being filled up from a SQL request. Now, I would like to know the max (and min) value of the first dimension.

    How should I do that?

    Thanks for any hints!

    Stef
  • Lumpy
    New Member
    • Oct 2007
    • 69

    #2
    Originally posted by luftikus143
    Hello,

    I have an array which is being filled up from a SQL request. Now, I would like to know the max (and min) value of the first dimension.

    How should I do that?

    Thanks for any hints!

    Stef

    I think you should be able to just use it telling it what dimension to look in. It's kind of hard for me to put this one in words, so let me just show you a quick example...
    [CODE=php]
    $test[0] = array(1,2,3,4,5 );
    $test[1] = array(6,7,8,9,0 );
    echo max($test[0]); ///would output a 5
    echo max($test[1]);///would output a 9
    [/CODE]

    You know what I am saying here? By telling it to look in dimension "0" or the first dimension it will only look through those values.

    Hope this helps!

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Stef.

      Probably the easiest way to do it would be to loop through the array and compare max() values:

      [code=php]
      $__max = 0;

      foreach( $__array as $__vals )
      {
      $__max = max( max($__vals), $__max);
      }
      [/code]

      This is a fairly CPU-intensive operation (esp. for large arrays). If you find that your script has to do this frequently, you might want to reconsider your process design.

      Comment

      • luftikus143
        New Member
        • Jan 2007
        • 97

        #4
        Great. Second solution works perfectly. Merci!!

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Stef.

          Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

          Comment

          Working...