array_count_values

(PHP 4, PHP 5)

array_count_values -- 统计数组中所有的值出现的次数

说明

array array_count_values ( array input )

array_count_values() 返回一个数组,该数组用 input 数组中的值作为键名,该值在 input 数组中出现的次数作为值。

例子 1. array_count_values() 例子

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

上例将输出:

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

参见 count()array_unique()array_values()count_chars()


add a note add a note User Contributed Notes
coda at bobandgeorge dot com
14-Oct-2005 08:52
alwaysdrunk's comment only works if you can trust the client web browser. Using this function doesn't validate that every necessary field exists -- only that every field that was submitted has a value in it. Thus if an attacker wished to force a null value into one of the fields, he could (rather easily) construct a modified form without the field and submit THAT.

Besides, you really ought to be validating each field anyway if you're taking user input.
alwaysdrunk at gmail com
12-May-2005 03:14
if you have too many values in $_POST,$_GET array that needs to be controlled with isset() in oreder to understand the form is filled completely and have no empty text boxes.
you can try this,it saves time.

<?
$n
= array_count_values($_POST);
if (!isset(
$n[''])) {
echo
"The form is filled completely";
}
else
{ die(
"Please fill the form comlpetely"); }

//tested in php 5
?>
[Mr.A]
06-Mar-2005 06:59
I find a very simple solution to count values in multidimentional arrays (example for 2 levels) :

foreach ($array as $a) {
  foreach ($a as $b) {
   $count_values[$b]++;
  }
}
blauauge at figh7club dot com
04-Sep-2003 12:12
my solution for count  on multidimentional arrays.

<?php
 
for($i = 0; $i < count($array); $i++) {
    
$detail = explode("|", $array[$i]);
     echo
"$i - $detail[0] - $detail[1]<br><br>";
     if(
$detail[1] == '1') { $wieoft1 = $wieoft1 +=1; }
     if(
$detail[1] == '2') { $wieoft2 = $wieoft2 +=1; }
     if(
$detail[1] == '3') { $wieoft3 = $wieoft3 +=1; }
  }
  echo
". $wieoft1 : $wieoft2 : $wieoft3";
?>

looks not pretty fine yet works great for me.
make it bigger for your own.
programmer at bardware dot de
20-Aug-2003 02:24
array_count_values returns the number of keys if empty(value). I expected array_count_values to return 0 for empty values.

Array looks like:
Array
       (
           [459] =>
           [543] =>
           [8959] =>
           [11273] =>
       )

array_count_values returns:
Array
(
   [] => 4
)

count(array_count_values(array)) does thus not report there are no values (other than empty) in the array.

I therefore check:
$arrFoo=array_count_values($arrBar);
if(isset($arrFoo[""]) $allempty=count($arrBar)==$arrFoo[""];
if(!$allempty)
//process the array
else
//no need to work on the array
digleu at codeway dot de
29-May-2003 05:58
I fount a solution for the count of array elements in the sense of array_count_values, but i was not able to use the function array_count_values itself because it does not say me if arrays exists in the given array, so i had to use a foreach loop and a little bit of recursivity ;)

<?php
function array_count_values_multidim($a,$out=false) {
  if (
$out===false) $out=array();
  if (
is_array($a)) {
   foreach(
$a as $e)
    
$out=array_count_values_multidim($e,$out);
  }
  else {
   if (
array_key_exists($a,$out))
    
$out[$a]++;
   else
    
$out[$a]=1;
  }
  return
$out;
}
?>
pmarcIatIgeneticsImedIharvardIedu
30-Jan-2003 09:42
array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
"array_count_values ($score)" return the error message "Warning: Can only count STRING and INTEGER values!".
manuzhai (AT) php (DOT) net
16-Nov-2002 10:45
You might use serialize() to serialize your objects before analyzing their frequency. :)
jon at fuck dot org
15-Aug-2002 05:31
suggested plan of attack:

<pre>
<?
class MyObject {
   function
MyObject($t = 'none')
   {
    
$this->$myTag = $t;
   }
}

$myArray = array();

for (
$i = 1 ; $i < 11 ; $i++)
{
  
$myobj = new MyObject( str_pad('n', $i, 'x') );
  
$myArray[ $myobj->$myTag ] = $myobj;
}

print_r( array_count_values(array_keys($myArray)) );
?>

to sum up:
assuming each instance of an object you create has some sort of tag, e.g.,

$this->$myTag=get_class($this)

..you should be set. objects dont have value to compare the way strings and integers do, so, $myTag's value is arbitrary.
tschneider at formel4 dot de
12-Jun-2002 10:09
This does not works with objects. If you have an array filled with objects, you can not count them.

Example:

<?php
$myArray
= array();
for (
$i = 0 ; $i < 10 ; $i++)
{
  
$myObject = new MyObject();
  
$myArray[$i] = $myObject;
}

echo (
array_count_values($myArray));
?>

This gives you:
Warning: Can only count STRING and INTEGER values...

Found no solution for this yet...