Array
(
[18] => Array
(
[0] => 137585189
[1] => 138053588
)
[19] => Array
(
[0] => 137626141
[1] => 137672213
[2] => 137718802
)
)
Array
(
[18] => Array
(
[0] => 137585189
[1] => 138053588
)
[19] => Array
(
[0] => 137626141
[1] => 137672213
[2] => 137718802
[3] => 137732801
)
)
这是以下结果:
foreach($value as $val){
echo '<pre>';
print_r($value);
echo '</pre>';
}
如何计算每个数组的键总数?
数组= 5
数组= 6
答案 0 :(得分:3)
您可以通过提供选项COUNT_RECURSIVE
来执行递归count。您可以通过简单计数
<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
// normal count
echo count($food); // output 2
// to count second level entries
echo (count($food,COUNT_RECURSIVE)-count($food,0)); //output 6
?>
答案 1 :(得分:1)
您可以像这样使用COUNT_RECURSIVE
count($arr, COUNT_RECURSIVE);
请注意,这包括内部数组本身,因此第一个数组最终为7。
要解决此问题,您只需减去count($arr)
答案 2 :(得分:1)
试试这个:
<?php
$array = array(array(array(137585189,138053588),array(137626141,137672213,137718802)), array(array(137585189,138053588), array(137626141,137672213,137718802,137732801)));
foreach($array as $val){
echo '<pre>';
print_r($val);
echo (count($val,COUNT_RECURSIVE)-count($val,0));
}
?>