Home > Article > Backend Development > PHP counts the words that appear most frequently in two data
This is a classic question that is often asked in interviews. It mainly tests the mastery of some unpopular functions of PHP.
Before answering this question, let’s learn two php array functions that are not commonly used.
1. array_count_values counts the number of values in the array
2. array_intersect_key finds the intersection of the keys of two arrays
Through these two functions, we can easily calculate the intersection of the two arrays What is the word that occurs most often at the same time?
The code is as follows:
<?php $arr = array('A', 'B', 'A', 'B', 'C'); $arr2 = array('C', 'B', 'A', 'D', 'A'); $arr_count = array_count_values($arr); $arr2_count = array_count_values($arr2); print_r($arr_count); print_r($arr2_count); $result = array_intersect_key($arr_count, $arr2_count); print_r($result);
Running result
More PHP related knowledge , please visit PHP Chinese website!
The above is the detailed content of PHP counts the words that appear most frequently in two data. For more information, please follow other related articles on the PHP Chinese website!