Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an array which looks like this:

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

But I'm struggling to get the counts I need in the right format. Basically what I wish to achieve is a count for each 'Activity' within each 'Time'. So for example I would like to output something like this:

Time:00 - Surfing 2; Kayaking 1
Time:15 - Surfing 1; Swimming 2
Time:45 - Surfing 1

etc.

I'm not sure if I should be looking at count functions or maybe foreach inside a foreach? But it has me stumped. Thanks in advance.

share|improve this question
1  
have you try anything? – Dave Dec 6 '16 at 12:00
1  
I fail to see how the input could lead to the desired output... – jeroen Dec 6 '16 at 12:04
    
Your question is not clear. – Pranav MS Dec 6 '16 at 12:18
    
@jeroen and pranav m.s - please see answers below ;-) – The Sumo Dec 6 '16 at 14:03
    
Yes, I'm sure "Swimming" and "45" will magicallly appear like that ;-) – jeroen Dec 6 '16 at 14:48
up vote 1 down vote accepted

My attempt appears to work:

$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;
share|improve this answer
    
This one seems to work best and is most flexible so I'll mark it as the answer. Thanks to everyone for the help! – The Sumo Dec 6 '16 at 13:36
    
Thank you, I'm glad you liked my answer. – H2ONOCK Dec 6 '16 at 13:53

You can try:

$timings = [];
foreach ($array as $activity) {
    $timings[$activity['Activity']][] = $activity['Time'];
}

$surfing = array_sum($timings['Surfing']);
share|improve this answer

If structure of nested arrays will not change You can do this:

$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]++;
}

End print_r($result) return this:

Array
(
[00] => Array
    (
        [Surfing] => 2
        [Kayaking] => 1
    )

[15] => Array
    (
        [Surfing] => 1
    )

)
share|improve this answer
$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;
     }
}

next step what ever you loop again the new_array to show all array the new array for every unique time its your choice, or if you need it to compare within new data you just can directly access the new array as ex below

echo $new_array['00']['Surfing']

then you will get the count for time 00 for surfing

good luck

share|improve this answer

I know I'm late, but here is my code. Outputs exactly what you wanted.

<?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

*/
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.