PHP中的多维数组计数

时间:2022-06-12 21:31:56

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?

谁能帮我?

7 个解决方案

#1


17  

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

#2


37  

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.

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

I've expanded the code to give an explained example because array_map might 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));

#3


13  

Use Count Recursive and 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]);

#4


2  

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

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

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

#5


0  

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

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

echo $countTickets . " tickets found.";

#6


0  

:)

$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

#7


-1  

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

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

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

#1


17  

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

#2


37  

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.

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

I've expanded the code to give an explained example because array_map might 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));

#3


13  

Use Count Recursive and 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]);

#4


2  

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

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

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

#5


0  

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

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

echo $countTickets . " tickets found.";

#6


0  

:)

$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

#7


-1  

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

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

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