PHP计算多维数组中的项目

时间:2011-11-27 07:48:18

标签: php arrays multidimensional-array

从下面的数组中可以看出,11月18日出现了三个元素,11月22日出现了另外两个元素。有人可以告诉我如何从这个数组中分别检索3和2的计数?基本上,我想得到一个像这样的结果:

2011年11月18日= 3件

2011年11月22日= 2项

当然,日期和不同日期的数量每次都会有所不同。这是数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [2011-11-18 00:00:00] => C
                )

            [1] => Array
                (
                    [2011-11-18 00:00:00] => I
                )

            [2] => Array
                (
                    [2011-11-18 00:00:00] => S
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [2011-11-22 00:00:00] => C
                )

            [1] => Array
                (
                    [2011-11-22 00:00:00] => S
                )

        )

)

9 个答案:

答案 0 :(得分:23)

您可以使用:

count($array, COUNT_RECURSIVE);

Count number of leaves in nested array tree

答案 1 :(得分:8)

这是否适合您的需要?

$dates = array(array(array("2011-11-18 00:00:00" => C), array("2011-11-18 00:00:00" => I),array
("2011-11-18 00:00:00" => S)),
array(array("2011-11-22 00:00:00" => C), array("2011-11-22 00:00:00" => S)));

$date_count = array();  // create an empty array

foreach($dates as $date) {  // go thought the first level
    foreach($date as $d) {  // go through the second level
        $key = array_keys($d);  // get our date
        // here we increment the value at this date
        // php will see it as 0 if it has not yet been initialized
        $date_count[$key[0]]++;
    }
}
    // show what we have
print_r($date_count);

打印:

Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 2 )

注意:这假设您在构建阵列时始终会获取数据,并且每个日期的格式都相同。如果您不能假设每个日期都将被格式化,这将是使用date()函数的简单转换。如果您不能假设您将获得与此类似的数据结构,那么解决这个问题的最佳方法可能是通过递归函数。

答案 2 :(得分:3)

已发布的答案对于您的代表示例是正确的,但我想添加另一个解决方案,无论您可以创建多少嵌套数组,这都将有效。它以递归方式迭代数组并计算所有子数组中的所有项。

它返回数组中项目的总数。在第二个参数中,您可以指定一个数组 引用,它将包含(嵌套)数组中每个唯一键的计数。

示例:

<?php
$deeply_nested = array(
                     'a' => 'x',
                     'b' => 'x',
                     'c' => 'x',
                     'd' => array(
                         'a' => 'x',
                         'b' => 'x',
                         'c' => array(
                             'a' => 'x',
                             'b' => 'x'
                         ),
                         'e' => 'x'
                    ) 
                );

function count_nested_array_keys(array &$a, array &$res=array()) {
    $i = 0;
    foreach ($a as $key=>$value) {
        if (is_array($value)) {
             $i += count_nested_array_keys($value, &$res);
        }
        else {
             if (!isset($res[$key]) $res[$key] = 0;

             $res[$key]++;
             $i++;
        }
    }
    return $i;
}

$total_item_count = count_nested_array_keys($deeply_nested, $count_per_key);

echo "total count of items: ", $total_item_count, "\n";
echo "count per key: ", print_r($count_per_key, 1), "\n";

结果:

total count of items: 8
count per key: Array
(
    [a] => 3
    [b] => 3
    [c] => 1
    [e] => 1
)

答案 3 :(得分:2)

假设您的数组示例具有代表性:

foreach ($array as $key => $value)
{
   echo count($value) . "<br />";
}

将回显每个主要数组项中的数组数。在您的示例中,这也是每个日期的条目数。

这当然不会检查日期本身

答案 4 :(得分:1)

您可以使用array_walk_recursive()访问阵列结构中所有叶子节点。

类似于此的东西应该对你有用:

<?php

$data = array(
 array(
  array('2011-11-18 00:00:00' => 'C'),
  array('2011-11-18 00:00:00' => 'I'),
  array('2011-11-18 00:00:00' => 'S')),
 array(
  array('2011-11-22 00:00:00' => 'C'),
  array('2011-11-22 00:00:00' => 'S')));

function countleafkeys($value, $key, $userData)
{
  echo "$key\n";
  if(!isset($userData[$key])) {
    $userData[$key] = 1;
  } else {
    $userData[$key]++;
  }
}

$result = array();
array_walk_recursive($data, 'countleafkeys', &$result);

print_r($result);

输出:

2011-11-18 00:00:00
2011-11-18 00:00:00
2011-11-18 00:00:00
2011-11-22 00:00:00
2011-11-22 00:00:00
Array
(
    [2011-11-18 00:00:00] => 3
    [2011-11-22 00:00:00] => 2
)

答案 5 :(得分:1)

对于您的特定$array结构,我认为最精益的方法是使用foreach,然后从每个值中获取日期值和count()

$dateCounts = array();
foreach($array as $date)
{
    $dateCounts[key($date[0])] = count($date);
}
var_dump($dateCounts);

使用$array,即可:

array(2) {
  ["2011-11-18 00:00:00"]=> int(3)
  ["2011-11-22 00:00:00"]=> int(2)
}

如果您正在寻找更通用的方法,可以使用RecursiveArrayIteratorRecursiveIteratorIterator遍历所有叶键/值元素,然后只计算键:

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$keyCounts = array();
foreach ($it as $key => $value)
{
    isset($keyCounts[$key]) ? $keyCounts[$key]++ : $keyCounts[$key] = 1; 
} 
var_dump($keyCounts);

希望这有帮助。

答案 6 :(得分:0)

这是我的递归变体:

$arr = array(
    '0' => array(
        '0' => array('2011-11-18 00:00:00' => 'C'),
        '1' => array('2011-11-18 00:00:00' => 'I'),
        '2' => array('2011-11-18 00:00:00' => 'S')
    ),
    '1' => array(
        '0' => array('2011-11-22 00:00:00' => 'C'),
        '1' => array('2011-11-22 00:00:00' => 'S')
    ),
    '2' => array(
        '0' => array(
            '0' => array('2011-11-22 00:00:00' => 'D')
        )
    )
);

function count_values($array, &$result = array(), $counter = 0)
{
    foreach ($array as $key => $data)
    {
        if (is_array($data))
        {
            count_values($data, $result, $counter);
        }
        else
        {
            array_key_exists($key, $result) ? $result[$key]++ : $result[$key] = 1;
        }
    }

    return $result;
}

print_r(count_values($arr));

这将返回:

Array ( [2011-11-18 00:00:00] => 3 [2011-11-22 00:00:00] => 3 )

答案 7 :(得分:0)

<?php
$count0=count($array[0], COUNT_RECURSIVE)-count($array[0]);
$count1=count($array[1], COUNT_RECURSIVE)-count($array[1]);

答案 8 :(得分:0)

如果您想要计算一维和二维的项目,可以尝试:

echo 'Size of unidimensional is: '.count($celda).'<br/>';

echo 'Size of bidimensional is: '.count($celda[0]).'<br/>';
相关问题