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

WBOY
Release: 2016-08-04 09:19:59
Original
1404 people have browsed it

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>
Copy after login
Related labels:
php
source:php.cn
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