Announcement

Collapse
No announcement yet.

avgval function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • avgval function

    I was watching Cube and came up with a idea for a function, i have no idea if it already exists though but it may come in helpful.
    PHP Code:
    function avgval($avg_vals) {
     if ( 
    is_array($avg_vals) && count($avg_vals) > ) {
            
    $return_vals = ( array_sum($avg_vals) / count($avg_vals) );
     } elseif ( 
    is_array($avg_vals) && count($avg_vals) == ) {
            
    $return_vals current($avg_vals);
     } else {
            
    $return_vals FALSE;
     }
       return 
    $return_vals;
    }
       echo 
    avgval(array(6,11,7)); // outputs 8
       
    echo avgval(array(6)); // outputs 6 

  • #2
    Also thought i'd make up another version which works on string rather than array (im sure the two could be collided).
    PHP Code:
    function avgvals($avg_vals,$avg_delimiter=',') {
     if ( (
    is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
        
    $average_vals explode($avg_delimiter$avg_vals);
            
    $return_vals = ( array_sum($average_vals) / count($average_vals) );
     } elseif ( (
    is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
            
    $return_vals $avg_vals;
     } else {
            
    $return_vals FALSE;
     }
       return 
    $return_vals;
    }
       echo 
    avgvals('6,11,7'); // outputs 8
       
    echo avgvals('6-11-7''-'); // outputs 8
       
    echo avgvals('6'); // outputs 6 

    Comment


    • #3
      This could actually be quite handy.
      Nice work Zero!
      "We believe that bad sex and good drugs are the cornerstones of a great democracy."
      - P.A.G.A.N
      Will you be (Mg,Fe)7Si8O22(OH)2 ?



      Comment


      • #4
        I submitted them to php.net in notes so im sure people will use them.

        Comment


        • #5
          PHP isn't new, neither is mathematics - everything you will ever think of has been done before.
          I find these two function to be more productive: http://www.mdj.us/web-development/ph...rray-with-php/

          Edit: Now that I think of it, why the hell doesn't PHP have an average or median function? When I learned Delphi my teacher drilled my with those functions(sqrt, cos and sin was more severe, but still).

          Comment


          • #6
            PHP Code:
            function calculate_average($arr) {
                
            $count count($arr); //total numbers in array
                
            foreach ($arr as $value) {
                    
            $total $total $value// total value of array numbers
                
            }
                
            $average = ($total/$count); // get average value
                
            return $average;

            compared to mine
            PHP Code:
            function avgval($avg_vals) {
             if ( 
            is_array($avg_vals) && count($avg_vals) > ) {
                    
            $return_vals = ( array_sum($avg_vals) / count($avg_vals) );
             } elseif ( 
            is_array($avg_vals) && count($avg_vals) == ) {
                    
            $return_vals current($avg_vals);
             } else {
                    
            $return_vals FALSE;
             }
               return 
            $return_vals;

            I'd say mine is better and more efficient really...

            Yeah i published my notes on php.net hope they maybe adopt the functions in some way that would be nice.

            Comment


            • #7
              Just so happens that earlier today I needed such a function. So thanks for this.
              Ruler of Zu

              FIA ISP FTW!

              MWG a discussion forum. If you do not like replies then perhaps dont start a topic.

              I no longer support Mccodes bugs, errors or security flaws either on the forum or by private message due to the lack of support by the mccodes staff for their own engine.

              If your looking for an engine get one thats supported by its staff. EzRPG and EzRPG Rework & NWE are my recommendations.

              Comment


              • #8
                Your welcome, glad i could help.

                Comment

                Working...
                X