How to unify a certain total in php

(*-*)浩
Release: 2023-02-25 15:36:02
Original
2623 people have browsed it

A simple loop is enough, select the repeated elements (the number of occurrences is greater than or equal to 2) and count the number of occurrences of each repeated element.

How to unify a certain total in php

##The array_count_values() function in PHP can be implemented

array_count_values()

The function is used to count the number of occurrences of all values ​​in the array. (Recommended learning: PHP video tutorial)

This function returns an array, the key name of its element is the value of the original array, and the key value is the value in the original array the number of times it appears.

array_count_values(array)
Copy after login

For example:

<?php
$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
?>
Copy after login

Output:

Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )
Copy after login

The above is the detailed content of How to unify a certain total in php. For more information, please follow other related articles on the PHP Chinese website!

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