PHP Method for counting the 10 most frequently occurring numbers in the numeric Array

Source: Internet
Author: User
This article mainly introduces the method for calculating the top 10 most frequently occurring numbers in the PHP numeric array. It involves the usage skills of array_count_values, arsort, and other methods in php, which is of great practical value, friends in need

This article mainly introduces the method for calculating the top 10 most frequently occurring numbers in the PHP numeric array. It involves the usage skills of array_count_values, arsort, and other methods in php, which is of great practical value, friends in need

This example describes how to count the 10 most frequently used numbers in the PHP numeric array. Share it with you for your reference. The specific analysis is as follows:

This problem is in the TOPK category. It is often used to count the frequency of word occurrence, report, and data statistics!

The php code is as follows:

// Randomly generate a value array for ($ I = 0; $ I <1000; $ I ++) {$ ary [] = rand );} // count the number of occurrences of all values in the array $ ary = array_count_values ($ ary); arsort ($ ary); // sort values in reverse order $ I = 1; foreach ($ ary as $ key = >$ value) {if ($ I <= 10) {printf ("number: % d appears % d in total
", $ Key, $ value) ;}else {break ;}$ I ++;} unset ($ ary );

The result is as follows:

Number: 255 6 times in total number: 443 5 times in total number: 906 5 times in total number: 623 5 times in total number: 586 4 times in total number: 660 appear 4 times: 873 appear 4 times: 208 appear 4 times: 247 appear 4 times: 240 appear 4 times: appear 4 times: appear 4 times

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.