Home  >  Article  >  Backend Development  >  How to unify a certain total in php

How to unify a certain total in php

(*-*)浩
(*-*)浩Original
2019-09-29 13:38:582621browse

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)

For example:

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

Output:

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

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!

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
Previous article:Is php a language?Next article:Is php a language?