Home  >  Article  >  Backend Development  >  PHP finds the number of occurrences of each element in an array

PHP finds the number of occurrences of each element in an array

WBOY
WBOYOriginal
2016-08-04 09:19:591403browse

I have a large array. I don’t need to bring my own method. How to do it?

Reply content:

I have a large array. I don’t need to bring my own method. How to do it?

Why not use PHP’s built-in function array_count_values

No need to bring your own methods

<code>    $tagList = array("Cat", "Dog", "Horse", "Dog");
    $count = array();
    foreach ($tagList as $tVal) {
        if (!isset($count[$tVal])) {
            $count[$tVal] = 1;
        } else {
            $count[$tVal]++;
        }

    }</code>
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