我有这个阵列:
Array
(
[boks_1] => Array
(
[tittel] => Test
[innhold] => This is a test text
[publish] => 2
)
[boks_2] => Array
(
[tittel] => Test 3
[innhold] => This is a text test
[publish] => 1
)
[boks_3] => Array
(
[tittel] => Kontakt oss
[innhold] => This is a test text
[publish] => 1
)
)
如何使用PHP
count()
来计算我的数组中出现多少次[publish] => 1
?我将使用该值来控制flexbox容器中divs
的宽度。 最佳答案
为了好玩:
$count = array_count_values(array_column($array, 'publish'))[1];
获取
publish
键的数组计算数值
使用索引获取
1
s的计数好吧,更有趣的是:
$count = count(array_keys(array_column($array, 'publish'), 1));
获取
[1]
键的数组获取值
publish
的数组键数一数数组
注意:如果
1
是字符串而不是整数,您可能希望将true
作为第三个参数传递给array_keys()
以获得更精确的结果,并使用'1'
而不是1
。