PHP计数多维数组项 [英] PHP count items in a multi-dimensional array

查看:97
本文介绍了PHP计数多维数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以从以下数组看到,有出现在11月18日三大要素,而另外两个元素出现在22月有人能告诉我怎样才能从这个数组检索3计数和2分别?基本上,我想结果是这样的事情结束了:

2011年11月18日= 3项

2011年11月22日= 2项

当然,日期和不同的日期的数量将变化每一次。这里是数组:

 阵列

    [0] =>排列
        (
            [0] =>排列
                (
                    [2011-11-18 00:00:00] => C
                )            [1] =>排列
                (
                    [2011-11-18 00:00:00] =>一世
                )            [2] =>排列
                (
                    [2011-11-18 00:00] =>小号
                )        )    [1] =>排列
        (
            [0] =>排列
                (
                    [2011-11-22 00:00:00] => C
                )            [1] =>排列
                (
                    [2011-11-22 00:00:00] =>小号
                )        ))


解决方案

这是否对你的工作需要什么?

  $日期=阵列(阵列(阵列(2011-11-18 00:00:00开始=> C),阵列(2011-11-18〇点:00= I标记),阵列
(2011-11-18 00:00:00=> S)),
阵列(阵列(2011-11-22 00:00:00=&℃),阵列(2011-11-22 00:00:00=> S)));$ date_count =阵列(); //创建一个空数组的foreach($日期为$日期){//去想过的第一级
    的foreach($日期$ D){//经过第二级
        $键= array_keys($ D); //获取我们的约会
        //这里我们在这个日期递增值
        // PHP将视它为0,如果它尚未初始化
        $ date_count [$键[0] ++;
    }
}
    //显示我们有什么
的print_r($ date_count);

打印:

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

请注意:这是假定你将永远是获取数据,你的结构阵列,并且每个日期将被格式化一样。如果你不能假设每个日期将被格式化,这将是使用date()函数的简单转换。如果你不能假设你会得到准确的结构是这样的数据,最好的办法来解决这很可能是通过递归函数。

As you can see from the following array, there are three elements that appear on Nov 18, and another two elements that appear on Nov 22. Can someone tell me how I can retrieve the counts of 3 and 2 respectively from this array? Basically, I want to end up with a result something like this:

Nov 18, 2011 = 3 items

Nov 22, 2011 = 2 items

Of course, the dates and the number of different dates will vary every time. Here is the array:

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
                )

        )

)

解决方案

Does this work for what you need?

$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);

Prints:

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

Note: this assumes that you will always be getting data as you structured your array and that each date will be formatted the same. If you can't assume each date will be formatted, this would be a simple conversion using the date() function. If you can't assume that you will get data structured exactly like this, the best way to tackle that would probably be through a recursive function.

这篇关于PHP计数多维数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆