php PHP中的多维数组计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9062770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:04:45  来源:igfitidea点击:

Multi-Dimensional array count in PHP

phpmultidimensional-array

提问by Kit Barnes

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.

我试图计算数组中的票总数(第二级项目的数量),但类型的数量是可变的,每张票的字段数也是如此,所以我不能使用 COUNT_RECURSIVE 和数学来计算得到号码。

Can anyone help me?

谁能帮我?

回答by bigkm

A bit late but this is a clean way to write it.

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

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

Assuming $ticketsis your multi-dimensional array.

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

I've expanded the code to give an explained example because array_mapmight be new to some

我已经扩展了代码以给出一个解释的例子,因为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

So because we don't care about the intermediate result of each type we can feed the result into array_sum

所以因为我们不关心每种类型的中间结果,所以我们可以将结果输入到array_sum 中

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

回答by Swadq

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

回答by Hyman M.

Use Count Recursiveand subtract the First level count, like this:

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

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

If your array has +3 levels, just add [?] keys:

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

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

回答by xhdix

:)

:)

$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

数组数 3

array count 2

数组数 2

array count 2

数组数 2

array count 7

数组数 7

array count 5

数组数 5

array count 4

数组数 4

回答by Vyktor

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

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

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

回答by Frosty Z

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

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

echo $countTickets . " tickets found.";

回答by Saiful Islam

To count total number of Ticket, this bellow code will help you for PHP.

要计算票证的总数,此波纹管代码将对您的 PHP 有所帮助。

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