PHP 中的多维数组计数

问题描述 投票:0回答:7

我有一个多维数组,设置如下

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 和数学获取号码。

有人可以帮助我吗?

php multidimensional-array
7个回答
70
投票

有点晚了,但这是一种干净的写法。

$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));

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

32
投票

使用 Count Recursive 并减去 First level count,如下所示:

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

如果你的数组有+3层,只需添加[?]键:

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

3
投票

:)

$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


2
投票

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

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

0
投票

您可以尝试使用

array_walk_recursive()

$totalTickets = 0;

array_walk_recursive($tickets, function($item) use (&$totalTickets) {
    $totalTickets++;
});

-1
投票

要计算门票总数,以下代码将帮助您使用 PHP。

foreach($mainArray as $Array){
    foreach($Array as $perTicke){
        $count++;
    }
}
$total_ticket = $count;
© www.soinside.com 2019 - 2024. All rights reserved.