Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:11:23+00:00 2026-06-13T11:11:23+00:00

I have the following array: [ratings] => Array ( [0] => 3 [1] =>

  • 0

I have the following array:

[ratings] => Array
 (
      [0] => 3
      [1] => 3
      [2] => 2
      [3] => 3
      [4] => 3
      [5] => 1
      [6] => 3
      [7] => 4
      [8] => 5
 )

What would be the best and fastest way to get the the percentage of each rating. For example there are 9 ratings now, and 5 ratings of “3”, so the percentage of rating “3” is 55.55%.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:11:24+00:00Added an answer on June 13, 2026 at 11:11 am

    although he probably has something already, this is what I came up with.

    <?php
    $array = array(3,3,2,3,3,1,3,4,5);
    
    function array_avg($array, $round=1){
        $num = count($array);
        return array_map(
            function($val) use ($num,$round){
                return array('count'=>$val,'avg'=>round($val/$num*100, $round));
            },
            array_count_values($array));
    }
    
    $avgs = array_avg($array);
    
    /*
     * You can access any average/count like:
     * echo "The count of 3 is: {$avgs[3]['count']}";
     * echo "The average of 3 is: {$avgs[3]['avg']}";
     */
    
    echo '<pre>'.print_r($avgs,1).'</pre>';
    

    Output:

    Array
    (
        [3] => Array
            (
                [count] => 5
                [avg] => 55.6
            )
    
        [2] => Array
            (
                [count] => 1
                [avg] => 11.1
            )
    
        [1] => Array
            (
                [count] => 1
                [avg] => 11.1
            )
    
        [4] => Array
            (
                [count] => 1
                [avg] => 11.1
            )
    
        [5] => Array
            (
                [count] => 1
                [avg] => 11.1
            )
    )
    

    http://codepad.viper-7.com/yD9CQm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array, I need to reorder it by average rating descending.
I have the following array(in python): arr= ['\n', ' a#B\n', ' c#D\n'] Now I
I have the following array of structures called test, where each field is a
I have the following array structure in MySQL. There could be no items or
I have following array that contains information on each file.but it is in single
I have the following array of days of the week, with each day having
I have following array, that I need to operate by hand on bitmaps. const
I have the following array: $franchise_a_status[] = array( 'id' => $franchise['franchise_id'], 'text' => $franchise['franchise_surname'].
I have taken following array; to fill-up gallery: private Integer[] mImageIds = { R.drawable.one,
I have the following array: Array ( [0] => BCD [1] => ACE [2]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.