Results 1 to 3 of 3

Thread: Average of values in a string? [Resolved]

  1. #1

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Resolved Average of values in a string? [Resolved]

    Hello all,
    I came across a problem with calc an average, the thing is, i have a big string, full of values separated using ; and ,
    Let me show:

    $myString="1,2;2,2;1,3;2,3;2,4";

    The average of that should return something like:

    (1+2+1+2+2)/5
    and
    (2+2+3+3+4)/5

    That string has 2 answers for 1 question, that would do 2 averages, not just one in this case.
    Answer 1 would be 1,6
    Answer 2 would be 2,8

    The answers schema is: <answer1>,<answer2>;<answer1>,<answer2>;...
    But it can be <answer1>,<answer2><answer3>;<answer1>,<answer2><answer3>;...
    or
    <answer1>;<answer1>;<answer1>;...

    Does anyone have any idea on how to solve this?
    Last edited by TDQWERTY; May 28th, 2005 at 02:02 AM.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Average of values in a string?

    Here is what you need to do:
    1. Use the split() function to turn the string into an array using a semi colon as a separator.

    2. For each string in this new array:
      1. Use the split() function again, but this time use the comma as a separator and you obtain an array of number.

      2. For each number in this array append it to a new array containing the answers for each question:
        PHP Code:
        $count count($answer_array);

        for(
        $x 0$x $count$x++) {
            
        $question_array[$x 1][] = $answer_array;


    3. Your question array should contain an index for each question number, each element being an array of answers. You can then loop through this array and calculate the average for each with the help of the array_sum() function.
      PHP Code:
      foreach($question_array as $question_number => $answers) {
          
      $average_array[$question_number] = array_sum($answers) / count($answers);

    4. You will now have an array of averages for each question.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Average of values in a string?

    thanks a lot!
    5 Points for your answer, my question has been answered at first!

    It works like a charm!
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width