Multi-Dimensional array count in PHP

2020-02-13 07:16发布

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

etc.

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.

Can anyone help me?

7条回答
家丑人穷心不美
2楼-- · 2020-02-13 07:53
$multiArrayTickets = array(...);
$countTickets = 0;

foreach($multiArrayTickets as $tickets)
  $countTickets += count($tickets);

echo $countTickets . " tickets found.";
查看更多
登录 后发表回答