这是我的数组
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
由此我需要子数组计数,即,具有两个索引的数组,例如来自此2 & 1
的{{1}},有一些嵌套数组,例如每个<{p}}
这里,我需要数组计数如下
2 & 1
我应该如何得到这个......
有人帮我解决了这个问题......
谢谢..
答案 0 :(得分:2)
使用计数或 sizeof 功能很容易预测阵列。
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
输出将作为您想要的输出
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
答案 1 :(得分:1)
这很简单,最好创建一个可以保存主数组项元素数的新数组:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
此外,您不需要为$counts
数组添加额外的数组,您得到的是:
array (
2 => 2,
1 => 1
)