我有一个看起来像这样的数组:
Array (
[0] => Array (
[Time] => 00
[Activity] => Surfing
)
[1] => Array (
[Time] => 00
[Activity] => Surfing
)
[2] => Array (
[Time] => 00
[Activity] => Kayaking
)
[3] => Array (
[Time] => 15
[Activity] => Surfing
)
)
但我正在努力以正确的格式获得我需要的计数。基本上我希望实现的是每个'时间'内每个'活动'的计数。所以例如我想输出这样的东西:
Time:00 - Surfing 2; Kayaking 1
Time:15 - Surfing 1; Swimming 2
Time:45 - Surfing 1
等
我不确定我是否应该关注计数函数或者在foreach中进行预测?但它让我难过。提前谢谢。
答案 0 :(得分:1)
我的尝试似乎有效:
$array = array(
array("time"=>"00", "Activity"=>"Surfing"),
array("time"=>"00", "Activity"=>"Surfing"),
array("time"=>"00", "Activity"=>"Kayaking"),
array("time"=>"15", "Activity"=>"Surfing")
);
$sortedArr = array();
/* first we need to sort the array so that we have a new array that is formatted with the time value as the key */
foreach($array as $arr) {
$timeVal = $arr['time'];
$activityName = $arr['Activity'];
/* if there is a time and activity key already set then we'll increment the count. Otherwise, we'll set it as 1 */
if(isset($sortedArr[$timeVal][$activityName])) {
$sortedArr[$timeVal][$activityName]++;
} else {
$sortedArr[$timeVal][$activityName] = 1;
}
}
/* now we've re-ordered things, we'll build the output and then echo it out */
$output = '';
foreach($sortedArr as $key=>$vals) {
$output.= 'Time:'.$key.' - ';
foreach($vals as $activityName=>$activityCount) {
$output .= $activityName.' '.$activityCount.';'.PHP_EOL;
}
}
echo $output;
答案 1 :(得分:0)
$new_array = array();
foreach ($array_list as $key => $val)
{
if(isset($new_array[$val[$time]][$val['Activity']]))
{
$new_array[$val[$time]][$val['Activity']] += 1;
}
else
{
$new_array[$val[$time]][$val['Activity']] = 1;
}
}
下一步你再次循环new_array,以便为你选择的每个唯一时间显示所有数组的新数组, 或者如果您需要在新数据中进行比较,您可以直接访问新数组,如下所示
echo $new_array['00']['Surfing']
然后你将获得冲浪时间00的计数
祝你好运答案 2 :(得分:0)
您可以尝试:
$timings = [];
foreach ($array as $activity) {
$timings[$activity['Activity']][] = $activity['Time'];
}
$surfing = array_sum($timings['Surfing']);
答案 3 :(得分:0)
如果嵌套数组的结构不会改变 你可以这样做:
$array = [
[
"Time" => "00",
"Activity" => "Surfing"
],
[
"Time" => "00",
"Activity" => "Surfing"
],
[
"Time" => "00",
"Activity" => "Kayaking"
],
[
"Time" => "15",
"Activity" => "Surfing"
]
];
$result = [];
foreach ($array as $key => $value) {
$time = $value["Time"];
$activity = $value["Activity"];
if (!isset($result[$time])) {
$result[$time] = [];
}
if (!isset($result[$time][$activity])) {
$result[$time][$activity] = 0;
}
$result[$time][$activity]++;
}
结束print_r($result)
返回此信息:
Array
(
[00] => Array
(
[Surfing] => 2
[Kayaking] => 1
)
[15] => Array
(
[Surfing] => 1
)
)
答案 4 :(得分:0)
我知道我迟到了,但这是我的代码。 输出正是您想要的。
<?php
//mockup
$activities = array(
array(
"Time" => "00",
"Activity" => "Surfing",
),
array(
"Time" => "00",
"Activity" => "Surfing",
),
array(
"Time" => "00",
"Activity" => "Kayaking",
),
array(
"Time" => "15",
"Activity" => "Surfing"
),
array(
"Time" => "15",
"Activity" => "Swimming"
),
array(
"Time" => "15",
"Activity" => "Swimming"
),
array(
"Time" => "45",
"Activity" => "Surfing"
)
);
$reordered = array();
foreach($activities as $activity)
{
foreach($activity as $key => $value)
{
//echo "$key => $value <br>";
if($key=="Time")
{
$tmp_time = $value;
}
elseif($key=="Activity")
{
if(!isset($reordered[$tmp_time][$value]))
$reordered[$tmp_time][$value]=1;
else
$reordered[$tmp_time][$value]++;
}
}
}
//var_dump($reordered);
$output='';
foreach($reordered as $key => $arr)
{
$output.='Time:' . $key . ' - ';
foreach($arr as $activityName=>$activityCount)
{
$output.= $activityName.' '.$activityCount.'; ';
}
$output = rtrim($output,"; ");
$output.=PHP_EOL;
}
echo $output;
/*
Time:00 - Surfing 2; Kayaking 1
Time:15 - Surfing 1; Swimming 2
Time:45 - Surfing 1
*/