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?
A bit late but this is a clean way to write it.
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
So because we don't care about the intermediate result of each type we can feed the result into array_sum
The easiest way to do this is one simple foreach loop:
:)
output:
array count 3
array count 2
array count 2
array count 7
array count 5
array count 4
Use Count Recursive and subtract the First level count, like this:
If your array has +3 levels, just add [?] keys:
To count total number of Ticket, this bellow code will help you for PHP.