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:40

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));
查看更多
闹够了就滚
3楼-- · 2020-02-13 07:42
$count = 0;
foreach ($array as $type) {
    $count+= count($type);
}
查看更多
可以哭但决不认输i
4楼-- · 2020-02-13 07:46

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

$count = 0;
foreach( $tickets as $ticketType){
    $count += count( $ticketType);
}
查看更多
可以哭但决不认输i
5楼-- · 2020-02-13 07:47

:)

$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

查看更多
该账号已被封号
6楼-- · 2020-02-13 07:50

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]);
查看更多
闹够了就滚
7楼-- · 2020-02-13 07:51

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;
查看更多
登录 后发表回答