如何计算数组中相同的值并将其存储到变量中? [英] How do i count same values in an array and store it to a variable?

查看:18
本文介绍了如何计算数组中相同的值并将其存储到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$items = explode(',',$product); // values is 4,2,4,2,2,4

$unique_items=array_unique($items); // gives me 4,2

接下来应该用什么代码给我 4 = 3 , 2 = 3 并将值的数量存储到变量中?

What code should be next to give me 4 = 3 , 2 = 3 and store the number of values to a variable?

推荐答案

参见:array_count_values

喜欢:

$occurences = array_count_values($items);
print_r($occurences);

输出:

Array
(
    [4] => 3
    [2] => 3
)

用法:

echo $occurences[4]; // outputs 3

这篇关于如何计算数组中相同的值并将其存储到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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