Home  >  Article  >  Backend Development  >  PHP array learning to find the value with the highest repetition rate

PHP array learning to find the value with the highest repetition rate

青灯夜游
青灯夜游Original
2021-08-10 17:12:534634browse

In the previous article, we introduced the method of counting the number of occurrences of characters in a string. If you are interested, you can click on the link to read → "PHP String Learning - Counting the Occurrences of Characters". Now that we have introduced the counting method of the number of characters, let's also take a look at arrays. This time we will show you how to count the number of occurrences of array elements. You can refer to it if you need it.

As the title says, the theme of this article is to count the number of occurrences of array elements, then compare the number of occurrences, find the element with the most occurrences (repetition rate), and output its value and number of occurrences.

Let’s take a look at the following example.

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$array = array(7,"hello",7,8,"world",9,10,10,"hello",10);
$frequency = array_count_values($array);   // 统计数组中所有值出现的次数
echo "每个元素对应的出现次数:";
var_dump($frequency);
echo "按照键值进行降序排序:";
arsort($frequency); // 按照键值对数组进行降序排序
var_dump($frequency);
$max_number = reset($frequency);//出现最多的次数值
$more_value = key($frequency);//出现次数最多的值
echo "数组中出现次数最多的值为:{$more_value},总共出现{$max_number}次";
?>

Let’s analyze the above code and introduce the role of each key function.

  • array_count_values($array)The function can count all the values ​​in the $array array and count the occurrences of each array element; it will return An associative array $frequency, the key name of its element is the value of the original array, and the key value is the number of times the value appears in the original array.

  • The order of the elements in the returned associative array $frequency refers to the order of the elements of the original array $array, so the elements are arranged scattered and irregular. Therefore, use arsort($frequency) to sort the array in descending order by key value, so that the element with the largest key value (number of occurrences) will be at the beginning of the array.

  • Then we can use reset($frequency) to point the internal pointer of the array to the first element of the $frequency array (the element is $ the current element of the frequency array), and returns the element, thus obtaining the number of occurrences of the element with the highest repetition rate in the original array.

  • Finally use key($frequency) to get the key name of the current element of the $frequency array, which is the value of the original array.

Look at the output result:

PHP array learning to find the value with the highest repetition rate

It can be seen that in the $arrays array, The element with the highest repetition rate (most occurrences) is the number "10", which appears three times in total.

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays: PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning to find the value with the highest repetition rate. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn