有什么聪明的方法可以计算数组(PHP)中唯一项目的数量? [英] What is a clever way to count number of unique items in array (PHP)?

查看:95
本文介绍了有什么聪明的方法可以计算数组(PHP)中唯一项目的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
数组中的唯一条目

Possible Duplicate:
Unique entries in an array

假设数组已排序,如何获得每个唯一项目的计数数量

Let assume the array is sorted, how can I get number of counts for each unique items

示例:

  $array = array ("bye", "bye", "bye", "hello", "hello");

输出:

    bye   = 3
    hello = 2 

推荐答案

如果要获取给定数组中指定列中唯一值的总数,请以简单整数(而不是另一个数组)的形式尝试简单的操作像这样:

If you want to get the total count of unique values in a specified column within a given array, as a simple integer (rather than another array) try something simple like this:

$uniqueCount = count(array_unique(array_column($data, 'column_name'))); 

// (where $data is your original array, and column_name is the column you want to cycle through to find the total unique values in whole array.)  

var_dump(array_count_values(array("bye", "bye", "bye", "hello", "hello")));

这篇关于有什么聪明的方法可以计算数组(PHP)中唯一项目的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆