PHP 8.4.2 Released!

array_count_values

(PHP 4, PHP 5, PHP 7, PHP 8)

array_count_valuesCompte les occurrences de chaque valeur distincte dans un tableau

Description

array_count_values(array $array): array

Retourne un tableau contenant les valeurs du tableau array (qui doivent être des entiers ou des chaîne de caractèress) comme clés et leur fréquence comme valeurs.

Liste de paramètres

array

Le tableau de valeurs à compter

Valeurs de retour

Retourne un tableau associatif de valeurs ayant les clés correspondant à array et leurs nombres comme valeurs.

Erreurs / Exceptions

Lance une alerte de niveau E_WARNING pour chaque élément qui n'est pas une chaîne de caractères ou un entier.

Exemples

Exemple #1 Exemple avec array_count_values()

<?php
$array
= array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

L'exemple ci-dessus va afficher :

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

Voir aussi

  • count() - Compte tous les éléments d'un tableau ou dans un objet Countable
  • array_unique() - Dédoublonne un tableau
  • array_values() - Retourne toutes les valeurs d'un tableau
  • count_chars() - Retourne des statistiques sur les caractères utilisés dans une chaîne

add a note

User Contributed Notes 3 notes

up
27
sergolucky96 at gmail dot com
7 years ago
Simple way to find number of items with specific values in multidimensional array:

<?php

$list
= [
[
'id' => 1, 'userId' => 5],
[
'id' => 2, 'userId' => 5],
[
'id' => 3, 'userId' => 6],
];
$userId = 5;

echo
array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2
?>
up
7
rabies dot dostojevski at gmail dot com
17 years ago
I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:

<pre><?php
function array_icount_values($array) {
$ret_array = array();
foreach(
$array as $value) {
foreach(
$ret_array as $key2 => $value2) {
if(
strtolower($key2) == strtolower($value)) {
$ret_array[$key2]++;
continue
2;
}
}
$ret_array[$value] = 1;
}
return
$ret_array;
}

$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar2 = array_count_values($ar); // Normal matching
$ar = array_icount_values($ar); // Case-insensitive matching
print_r($ar2);
print_r($ar);
?></pre>

This prints:

Array
(
[J. Karjalainen] => 3
[60] => 2
[j. karjalainen] => 1
[Fastway] => 2
[FASTWAY] => 1
[fastway] => 1
[YUP] => 1
)
Array
(
[J. Karjalainen] => 4
[60] => 2
[Fastway] => 4
[YUP] => 1
)

I don't know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.
up
6
szczepan.krolgmail.c0m
14 years ago
Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.

<?php

$ar1
[] = array("red","green","yellow","blue");
$ar1[] = array("green","yellow","brown","red","white","yellow");
$ar1[] = array("red","green","brown","blue","black","yellow");
#$ar1= array("red","green","brown","blue","black","red","green"); // Possible with one or multiple Array

$res = array_icount_values ($ar1);
print_r($res);

function
array_icount_values($arr,$lower=true) {
$arr2=array();
if(!
is_array($arr['0'])){$arr=array($arr);}
foreach(
$arr as $k=> $v){
foreach(
$v as $v2){
if(
$lower==true) {$v2=strtolower($v2);}
if(!isset(
$arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return
$arr2;
}
/*
Will print:
Array
(
[red] => 3
[green] => 3
[yellow] => 4
[blue] => 2
[brown] => 2
[white] => 1
[black] => 1
)
*/
?>
To Top