I have a multi-dimentional array set up as follows
array() {
[\"type1\"] =>
array() {
[\"ticket1\"] =>
array(9) {
A bit late but this is a clean way to write it.
$totalTickets = array_sum(array_map("count", $tickets));
Assuming $tickets
is your multi-dimensional array.
I've expanded the code to give an explained example because array_map might be new to some
$tickets = array(
"type1" => array(
"ticket1" => array(),
"ticket2" => array(),
"ticket3" => array(),
),
"type2" => array(
"ticket4" => array(),
"ticket5" => array(),
"ticket6" => array(),
"ticket7" => array(),
),
"type3" => array(
"ticket8" => array()
)
);
// First we map count over every first level item in the array
// giving us the total number of tickets for each type.
$typeTotals = array_map("count", $tickets);
// print_r($typeTotals);
// $type_totals --> Array (
// [type1] => 3,
// [type2] => 4,
// [type3] => 1
// )
//Then we sum the values of each of these
$totalTickets = array_sum($typeTotals);
print($totalTickets);
// $totalTickets --> 8
So because we don't care about the intermediate result of each type we can feed the result into array_sum
$totalTickets = array_sum(array_map("count", $tickets));
Use Count Recursive and subtract the First level count, like this:
count($mainArr, COUNT_RECURSIVE) - count($mainArr);
If your array has +3 levels, just add [?] keys:
count($mainArr[1], COUNT_RECURSIVE) - count($mainArr[1]);
$multiArrayTickets = array(...);
$countTickets = 0;
foreach($multiArrayTickets as $tickets)
$countTickets += count($tickets);
echo $countTickets . " tickets found.";
:)
$test = array (
array (
'test','test','test'
),
array (
'test','test'
),
array (
array (
'test'
),
array (
'test','test','test','test'
)
)
);
echo " array count ". count($test[0]) ." <br /> ";
echo " array count ". count($test[1]) ." <br /> ";
echo " array count ". count($test[2]) ." <br /> ";
echo " array count ". count($test[2],1) ." <br /> ";
echo " array count ". (count($test[2],1) - count($test[2])) ." <br /> "; // all child num - parent num
echo " array count ". count($test[2][1]) ." <br /> ";
output:
array count 3
array count 2
array count 2
array count 7
array count 5
array count 4
To count total number of Ticket, this bellow code will help you for PHP.
foreach($mainArray as $Array){
foreach($Array as $perTicke){
$count++;
}
}
$total_ticket = $count;
$count = 0;
foreach ($array as $type) {
$count+= count($type);
}