我需要计算键/变量在数组数组中显示的次数。
数组看起来像这样:
Array
{
[3] => Array
{
[type]=>group
[name]=>3st group
[newmsgs]=>3
}
[2] => Array
{
[type]=>group
[name]=>2nd group
}
[1] => Array
{
[type]=>group
[name]=>1st group
[newmsgs]=>1
}
}
我正在寻找一个运行并返回 2 的函数,因为只有 2 个数组具有“newmsgs”键和值。
我尝试了 array_count_values()
但没有成功,还尝试了一个简单的 count()
,我知道它工作的机会很小。
知道如何做到这一点吗?
最佳答案
提取所需的列并对其进行计数:
$count = count(array_column($array, 'newmsgs'));
关于php - 需要找到具有特定键的数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190099/