PHP中的多维数组计数 [英] Multi-Dimensional array count in PHP

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

问题描述

我有一个多维数组,设置如下

I have a multi-dimentional array set up as follows

array() {
    ["type1"] =>
    array() {
        ["ticket1"] =>
        array(9) { 
           // ticket details here
        }
        ["ticket2"] =>
        array(10) { 
           // ticket details here
        }
        ["ticket3"] =>
        array(9) { 
           // ticket details here
        }
    }
    ["type2"] =>
    array() {
        ["ticket1"] =>
        array(9) { 
           // ticket details here
        }
        ["ticket2"] =>
        array(10) { 
           // ticket details here
        }
        ["ticket3"] =>
        array(9) { 
           // ticket details here
        }
    }
}

我正在尝试获取数组中票证总数(第二级项目数)的计数,但是类型数和每个票证具有的字段数一样是可变的,所以我不能使用COUNT_RECURSIVE和数学来获取数字.

I am trying to get a count of the total number of tickets in the array (number of second level items), but the number of types is variable as are the number of fields that each ticket has, so I cannot use COUNT_RECURSIVE and maths to get the number.

有人可以帮助我吗?

推荐答案

$count = 0;
foreach ($array as $type) {
    $count+= count($type);
}

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

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