如何计算数组中的varians数据? [英] How to counting varians data from array?

查看:56
本文介绍了如何计算数组中的varians数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组

$arr = array(1,2,1,3,2,4);

如何计算该数组中的变体数据总数?

How to count total of variant data from that array ?

示例:

$arr = array(1,2,1,3,2,4);

总数= 4

推荐答案

您可以翻转数组,并检查其长度:

You can flip the array, and check its length:

echo count(array_flip($arr));

之所以可行,是因为数组索引必须是唯一的,所以对于原始数组中的每个唯一项,最终都会得到一个元素:

This works because an array index must be unique, so you end up with one element for every unique item in the original array:

http://php.net/manual/en/function.array-flip.php

如果某个值多次出现,则将最新的键用作其值,其他所有键都将丢失.

If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

这是()吗?比 array_unique 快一些,但是除非您将其称为LOT,否则 array_unique 的描述性要强得多,所以可能是更好的选择

This is (was?) somewhat faster than array_unique, but unless you are calling this a LOT, array_unique is a lot more descriptive, so probably the better option

这篇关于如何计算数组中的varians数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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