计算每个子数组中的项目以生成唯一键和计数的关联数组
从下面的数组中可以看到,有 3 个元素出现在 11 月 18 日,另外两个元素出现在 11 月 22 日。有人能告诉我如何从该数组中分别检索 3 和 2 的计数吗?基本上,我想得到这样的结果:
2011 年 11 月 18 日 = 3 个项目
2011 年 11 月 22 日 = 2 个项目
当然,日期和不同日期的数量每次都会有所不同。这是数组:
[
[
['2011-11-18 00:00:00' => 'C'],
['2011-11-18 00:00:00' => 'I'],
['2011-11-18 00:00:00' => 'S']
],
[
['2011-11-22 00:00:00' => 'C'],
['2011-11-22 00:00:00' => 'S']
]
]
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:
[
[
['2011-11-18 00:00:00' => 'C'],
['2011-11-18 00:00:00' => 'I'],
['2011-11-18 00:00:00' => 'S']
],
[
['2011-11-22 00:00:00' => 'C'],
['2011-11-22 00:00:00' => 'S']
]
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用:
计算嵌套数组树中的叶子数量
You can use:
Count number of leaves in nested array tree
这能满足您的需要吗?
打印:
注意:这假设您在构造数组时始终会获取数据,并且每个日期的格式都相同。如果您不能假设每个日期都会被格式化,那么这将是使用 date() 函数的简单转换。如果您不能假设您将获得与此完全相同的数据结构,那么解决该问题的最佳方法可能是通过递归函数。
Does this work for what you need?
Prints:
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.
发布的答案对于您的代表性示例来说是正确的,但我想添加另一个解决方案,无论您可能创建多少个嵌套数组,该解决方案都将起作用。它递归地迭代数组并计算所有子数组中的所有项目。
它返回数组中项目的总数。在第二个参数中,您可以指定一个数组
引用将包含(嵌套)数组中每个唯一键的计数。
示例:
结果:
the posted answers are correct for your representative example, but i would like to add another solution, that will work regardless how many nested arrays you may create. it iterates the array recursively and counts all items in all sub-arrays.
it returns the total count of items in the array. in the second argument you can specify an array
reference which will contain the count per unique key in the (nested) array(s).
example:
results in:
假设您的数组示例具有代表性:
将回显每个主数组项中的数组数量。在您的示例中,这也是每个日期的条目数。
这当然不会检查日期本身
Assuming that your array example is representative:
Will echo the number of arrays within each of the main array items. In your example, that would also be the number of entries for each date.
This does not of course check the dates themselves
对于您的特定
$array
结构,我认为最精简的方法是使用foreach
,然后从每个中获取日期值和count()
值:使用您的
$array
给出:如果您正在寻找更通用的方法,您可以使用
RecursiveArrayIterator
和RecursiveIteratorIterator
来遍历所有叶子键/值元素,然后只计算键:希望这会有所帮助。
For your specific
$array
structure I think the most lean way is usingforeach
and then getting the date value and thecount()
out of each value:With your
$array
this gives:If you're looking for a more general way, you can make use of
RecursiveArrayIterator
andRecursiveIteratorIterator
to traverse over all leaf key/value elements and then just count the keys:Hope this helps.
您可以使用
array_walk_recursive()
访问数组结构中的所有叶节点。类似的东西应该适合你:
输出:
You can use
array_walk_recursive()
to get access to all of the leaf nodes in an array structure.Something akin to this should work for you:
Outputs:
这是我的递归变体:
这将返回:
Here is my recursive variant:
This will return:
给这只猫剥皮的方法有很多。我认为这取决于假设没有基准测试/性能要求的代码样式偏好。
所有这些脚本都会产生相同的结果。
经典嵌套循环(演示)
key()
和count ()
和一个经典循环(演示)使用 RecursiveIterator 的经典循环(演示)
array_walk_recursive()
(演示)COUNT_RECURSIVE
然后展平(演示)array_reduce() (演示)
展平、映射键,然后对值进行计数(演示)
So so many ways to skin this cat. I reckon it comes down to code styling preference assuming no benchmarking/performance requirements.
All of these scripts produce the same result.
Classic nested loops (Demo)
key()
andcount()
with one classic loop (Demo)Classic loop with RecursiveIterator (Demo)
array_walk_recursive()
(Demo)COUNT_RECURSIVE
then flatten (Demo)array_reduce()
(Demo)Flatten, map keys, then count values (Demo)
如果你想计算一维和二维的项目,你可以尝试:
If you want count the items unidimensional and bidimensional you can try: