PHP counts the words that appear most frequently in two data

angryTom
Release: 2023-04-07 16:18:01
forward
2006 people have browsed it

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(&#39;A&#39;, &#39;B&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;);
$arr2 = array(&#39;C&#39;, &#39;B&#39;, &#39;A&#39;, &#39;D&#39;, &#39;A&#39;);
$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);
Copy after login

Running result

PHP counts the words that appear most frequently in two data

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!

Related labels:
source:www.80shihua.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!