Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计算数组中每种类型的条目数?_Php_Arrays_Count_Find Occurrences - Fatal编程技术网

Php 如何计算数组中每种类型的条目数?

Php 如何计算数组中每种类型的条目数?,php,arrays,count,find-occurrences,Php,Arrays,Count,Find Occurrences,我需要计算角色1、2、3、4、5在此数组中存在的次数,例如,对于此,我应该得到答案 1-2次 2-2次 3-一次 4-3次 <?php array(8) { [0]=> array(2) { [0]=> string(1) "3" ["answer"]=> string(1) "3" } [1]=> array(2) { [0]=> string(1) "4" ["answer"]=&

我需要计算角色1、2、3、4、5在此数组中存在的次数,例如,对于此,我应该得到答案 1-2次 2-2次 3-一次 4-3次

<?php
array(8) {
  [0]=>
  array(2) {
    [0]=>
    string(1) "3"
    ["answer"]=>
    string(1) "3"
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
  [2]=>
  array(2) {
    [0]=>
    string(1) "1"
    ["answer"]=>
    string(1) "1"
  }
  [3]=>
  array(2) {
    [0]=>
    string(1) "1"
    ["answer"]=>
    string(1) "1"
  }
  [4]=>
  array(2) {
    [0]=>
    string(1) "2"
    ["answer"]=>
    string(1) "2"
  }
  [5]=>
  array(2) {
    [0]=>
    string(1) "2"
    ["answer"]=>
    string(1) "2"
  }
  [6]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
  [7]=>
  array(2) {
    [0]=>
    string(1) "4"
    ["answer"]=>
    string(1) "4"
  }
}
?>

如果这是从数据库返回的数据,为什么不使用数据库查询为您进行计数

SELECT answer,
       COUNT(1) AS instances
  FROM myTable
 GROUP BY answer

你想用什么语言来做这件事?这看起来像PHP。如果是,请将php标记添加到您的问题中。使用print\r可获得更清晰的输出。。。