PHP中的多维数组计数

时间:2012-01-30 10:56:58

标签: php multidimensional-array

我有一个多维数组,如下所示

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

我正在尝试计算数组中的票证总数(二级项目数),但是类型的数量是可变的,每个票证的字段数也是如此,所以我不能使用COUNT_RECURSIVE和数学得到数字。

任何人都可以帮助我吗?

7 个答案:

答案 0 :(得分:43)

有点晚了,但这是一种干净的写作方式。

$totalTickets = array_sum(array_map("count", $tickets));

假设$tickets是你的多维数组。

我已经扩展了代码以提供解释示例,因为array_map可能是某些

的新内容
$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

因为我们不关心每种类型的中间结果,我们可以将结果提供给array_sum

$totalTickets = array_sum(array_map("count", $tickets));

答案 1 :(得分:21)

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

答案 2 :(得分:16)

使用 Count Recursive 并减去第一级计数,如下所示:

count($mainArr, COUNT_RECURSIVE) - count($mainArr);

如果您的阵列有+3级别,只需添加[?]键:

count($mainArr[1], COUNT_RECURSIVE) - count($mainArr[1]);

答案 3 :(得分:2)

最简单的方法是使用一个简单的foreach循环:

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

答案 4 :(得分:2)

:)

$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 /> ";

输出:

数组计数3

数组计数2

数组计数2

数组计数7

数组计数5

数组计数4

答案 5 :(得分:0)

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

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

echo $countTickets . " tickets found.";

答案 6 :(得分:-1)

要计算Ticket的总数,这个波纹管代码将帮助您使用PHP。

foreach($mainArray as $Array){
    foreach($Array as $perTicke){
        $count++;
    }
}
$total_ticket = $count;