Skip to main content

Multi-Dimensional array count in 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

}

}

}







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?


Comments

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

    ReplyDelete
  2. The easiest way to do this is one simple foreach loop:

    $count = 0;
    foreach( $tickets as $ticketType){
    $count += count( $ticketType);
    }

    ReplyDelete
  3. $multiArrayTickets = array(...);
    $countTickets = 0;

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

    echo $countTickets . " tickets found.";

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?