SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help To Count Number and then Grouping The Result From Text Area

    Hai ....
    I'm really newbie here, but i wish you could help me to create some PHP Script that could counting the number and then grouping the result by the number.
    Ex:
    in textarea i write this number 1228323991237412398012312381038210382180328103241283092183012803 and then when i klik the go button, the result is :
    Numbers | Count
    2 14
    3 13
    1 12
    8 10
    0 8
    9 4
    4 2
    7 1

    Thank's you ...

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    tally your results into an array using the number as the index, and the count as the value. Then sort the array by index(key) and then echo out each element of the array.
    $nums = $_POST['mytextarea']; // get the value from the textarea
    $nums_ar = str_split($nums,1);
    $results_ar = array();
    foreach ($nums_ar as $val)
    {
    if (array_key_exists($results_ar["$val"]))
    $results_ar["$val"]++;
    else
    $results_ar["$val"] = 1;
    }
    ksort($results_ar)
    foreach($results_ar as $k=>$v)
    echo "$k occurred $v times<br>";

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    I'd do it a -little- bit more streamlined, but same methodology!

    $result = array_count_values(str_split($_POST['mytextarea']));
    ksort($result);
    foreach($result as $k=>$v)
    echo "$k occurred $v times<br>";

    (Hooray for inbuilt functions!)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

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