如何在一个数组数相同的价值观,并将其存储到一个变量? [英] How do i count same values in an array and store it to a variable?

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

问题描述

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

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

什么code应该下一个给我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天全站免登陆