如何计算数组中数组中的项目? [英] How to count items in an array that's in an array?

查看:71
本文介绍了如何计算数组中数组中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算数组中属于数组的项目?我在WordPress中使用Advance Custom Fields插件,数组 print_r(get_field('irl_today_entry'))输出看起来像这样:

How do you count items in an array that's part of an array? I'm using Advance Custom Fields plugin in WordPress and my array print_r(get_field('irl_today_entry')) output looks like this:

Array
(
[0] => Array
    (
        [acf_fc_layout] => irl_today_website_entry
        [irl_today_website] => Array
            (
                [0] => Array
                    ( data removed)

                [1] => Array
                    ( data removed )
            )
    )
[1] => Array
    (
        [acf_fc_layout] => irl_today_social_entry
        [irl_today_social] => Array
            (
                [0] => Array
                    ( data remove )
                [1] => Array
                    ( data remove)
            )
    )
)

如何只计算[irl_today_social]中的项目?我尝试了很多不起作用的选项。

How do you only count items in [irl_today_social]? I've tried a lot of options that do not work.

推荐答案

如果您有多个带有的条目irl_today_social ,您还可以使用 array_map array_column

If you have multiple entries with irl_today_social you might also use array_map and array_column

$res = array_map(function($x) {
    return count($x);
}, array_column($arrays, "irl_today_social"));

print_r($res);

输出

Array
(
    [0] => 2
)

查看 php演示

这篇关于如何计算数组中数组中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆