I have a an array like below:
我有一个像下面这样的数组:
$array = Array
(
'0' => Array
(
'num1' => 123,
'num2' => 456,
),
'1' => Array
(
'num3' => 789,
'num4' => 147,
),
'2' => Array
(
'num5' => 258,
'num6' => 369,
'num7' => 987,
),
);
I want to count number of elements i.e. from num1
to num7
means I want output 7
. How can i do this without using loop?
我想计算元素的数量,即从num1到num7意味着我想要输出7.如何在不使用循环的情况下执行此操作?
2 个解决方案
#1
5
use array_sum
and array_map
function together.
一起使用array_sum和array_map函数。
try below solution:
尝试以下解决方案
$array = Array
(
'0' => Array
(
'num1' => 123,
'num2' => 456,
),
'1' => Array
(
'num3' => 789,
'num4' => 147,
),
'2' => Array
(
'num5' => 258,
'num6' => 369,
'num7' => 987,
),
);
echo $total = array_sum(array_map("count", $array));
output
7
alternat way can be:
交替方式可以是:
echo count($array, COUNT_RECURSIVE) - count($array); //output: 7
#2
2
Using array_sum function
使用array_sum函数
$totalarray = array_sum(array_map("count", $array));
Using Foreach Loop
使用Foreach循环
$count = 0;
foreach( $array as $arrayVal){
$count += count($arrayVal);
}
#1
5
use array_sum
and array_map
function together.
一起使用array_sum和array_map函数。
try below solution:
尝试以下解决方案
$array = Array
(
'0' => Array
(
'num1' => 123,
'num2' => 456,
),
'1' => Array
(
'num3' => 789,
'num4' => 147,
),
'2' => Array
(
'num5' => 258,
'num6' => 369,
'num7' => 987,
),
);
echo $total = array_sum(array_map("count", $array));
output
7
alternat way can be:
交替方式可以是:
echo count($array, COUNT_RECURSIVE) - count($array); //output: 7
#2
2
Using array_sum function
使用array_sum函数
$totalarray = array_sum(array_map("count", $array));
Using Foreach Loop
使用Foreach循环
$count = 0;
foreach( $array as $arrayVal){
$count += count($arrayVal);
}