我有这个阵列:

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
        )
)

如何使用PHPcount()来计算我的数组中出现多少次[publish] => 1?我将使用该值来控制flexbox容器中divs的宽度。

最佳答案

为了好玩:

$count = array_count_values(array_column($array, 'publish'))[1];

获取publish键的数组
计算数值
使用索引获取1s的计数
好吧,更有趣的是:
$count = count(array_keys(array_column($array, 'publish'), 1));

获取[1]键的数组
获取值publish的数组键
数一数数组
注意:如果1是字符串而不是整数,您可能希望将true作为第三个参数传递给array_keys()以获得更精确的结果,并使用'1'而不是1

09-11 17:44
查看更多