我有一个数组Array( [activity] => Array ( [0] => Array ( [action] => open [timestamp] => 2019-08-02T21:34:03+00:00 ) [1] => Array ( [action] => open [timestamp] => 2019-08-02T20:27:54+00:00 ) [2] => Array ( [action] => click [timestamp] => 2019-08-02T20:27:54+00:00 ) [3] => Array ( [action] => open [timestamp] => 2019-08-02T20:26:43+00:00 ) ))我想计算 action=open 的动作总数。所以结果将是 3
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
您可以尝试使用array-column和array-count-values作为:
$actions = array_column($arr['activity'], 'action');
$cnts = array_count_values($actions );
现在只需将“打开”打印为:
echo $cnts['open'];
你也可以做简单的循环:
$cnt = 0;
foreach($arr['activity'] as $e) {
if ($e['action'] == 'open') $cnt++;
if ($cnt == 1) echo "First open at: " . $e['timestamp']; // print the date of the first open action
}
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报
0/150
提交
取消