2
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

4

3 回答 3

3

您可以通过提供选项进行递归计数COUNT_RECURSIVE。你可以通过简单的计数减去递归计数来得到你想要的

<?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

?>
于 2013-05-22T03:47:44.413 回答
1

你可以COUNT_RECURSIVE这样使用

count($arr, COUNT_RECURSIVE);

请注意,这包括内部数组本身,因此第一个数组最终将是 7。要解决这个问题,你可以减去count($arr)

于 2013-05-22T03:47:06.737 回答
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));

        }

?>

在这里演示>>

于 2013-05-22T03:53:47.110 回答