php – 计算数组中键的值

我有这个数组:

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容器中div的宽度.

解决方法:

为了娱乐:

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

>获取一组发布密钥
>计算值
>使用索引[1]获取1的计数

好.更多乐趣:

$count = count(array_keys(array_column($array, 'publish'), 1));

>获取一组发布密钥
>获取值为1的数组键
>计算数组

注意:您可能希望将true作为array_keys()的第三个参数传递给更准确,如果1是字符串而不是整数,则使用“1”而不是1.

上一篇:c – 如何在一元谓词中使用类成员变量?


下一篇:c – std :: greater()和std :: greater之间的区别?