2

I am given an array of arrays, to simplify each array has

[0] => Array
(
[target_id]
[target_date]
[intensity_id]
[target_intensity] 
)
[1] => Array
(
[target_id]
[target_date]
[intensity_id]
[target_intensity] 
)

example data (the original array is an assoc array)

target_id target_date intensity_id target_intensity
1829    2017-06-23  2       good 
2013    2017-05-22  1       bad 
2024    2017-05-18  1       bad 
2029    2017-05-14  1       bad 
2032    2017-05-10  3       bad 
2329    2017-03-23  2       good 
2629    2017-03-12  3       excellent 
2830    2017-02-03  2       good 
3829    2017-02-23  2       good 
4029    2017-02-18  1       bad 
3829    2017-01-23  2       good 
3829    2016-12-12  3       excellent 
3829    2016-12-23  2       good 

now i need to echo this data as input for a google stacked bar graph.

So I need to get the sum (number of times an intensity appears in a month) of target_intensity by month, like so

date        bad good    excellent
2016 dec     0  1   1
2017 jan    0   1   0
2017 feb    1   2   0
2017 mrt    0   1   1
2017 may    3   0   1
2017 jun    0   1   0

and here I am stuck

$tmpArray = array(
    array('target_id' => 1829, 'target_date' => '2017-06-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 2013, 'target_date' => '2017-05-22', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2024, 'target_date' => '2017-05-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2029, 'target_date' => '2017-05-14', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2032, 'target_date' => '2017-05-10', 'intensity_id'=>3, 'target_intensity'=>'bad'),
    array('target_id' => 2329, 'target_date' => '2017-03-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 2629, 'target_date' => '2017-03-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
    array('target_id' => 2830, 'target_date' => '2017-02-03', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 3829, 'target_date' => '2017-02-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 4029, 'target_date' => '2017-02-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 4039, 'target_date' => '2017-01-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 4049, 'target_date' => '2016-12-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
    array('target_id' => 4056, 'target_date' => '2016-12-23', 'intensity_id'=>2, 'target_intensity'=>'good')
);

//echo $tmpArray[3]['target_date'];
//exit();
// should i first loop and order by month and then
//should i create 3 tmp arrays for bad, good and excellent


foreach ($tmpArray as $key => $values) {
    echo $values['target_date'] . '<br/>';
    //print_r ($values).'<br/>';
    foreach ($values as $key => $value) {
//        echo '<pre>';
        echo $key . ' - ' . $value . '<br/>';
//        echo '</pre>';
    }
    echo '<hr/>';
}

Any help appreciated

2
  • 1
    year 2016 has only excellent and good intensity, so why did you specify bad: 1 in your output for 2016 dec ? Commented Dec 15, 2017 at 13:33
  • typo ..very sharp of you ....i am looking at this for quit awhile and i am making stupid mistakes Commented Dec 15, 2017 at 13:36

2 Answers 2

1

Hope this will help you: Here I am just grouping the result to make the array

<?php
$array = array(
    array('target_id' => 1829, 'target_date' => '2017-06-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 2013, 'target_date' => '2017-05-22', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2024, 'target_date' => '2017-05-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2029, 'target_date' => '2017-05-14', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 2032, 'target_date' => '2017-05-10', 'intensity_id'=>3, 'target_intensity'=>'bad'),
    array('target_id' => 2329, 'target_date' => '2017-03-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 2629, 'target_date' => '2017-03-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
    array('target_id' => 2830, 'target_date' => '2017-02-03', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 3829, 'target_date' => '2017-02-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 4029, 'target_date' => '2017-02-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
    array('target_id' => 4039, 'target_date' => '2017-01-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
    array('target_id' => 4049, 'target_date' => '2016-12-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
    array('target_id' => 4056, 'target_date' => '2016-12-23', 'intensity_id'=>2, 'target_intensity'=>'good')
);

$output = [];
foreach($array as $value) {
    $year = date('Y', strtotime($value['target_date']));
    $month = date('M', strtotime($value['target_date']));

    if(empty($output[$year][$month]))
    $output[$year][$month] = ['bad' => 0, 'good' => 0, 'excellent' => 0];

    $output[$year][$month][$value['target_intensity']] += 1;
}

ksort($output);
ksort($output[$year]);

echo "<pre>";
print_r($output);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

ksort($output[$year]); ? So you only want to sort that last iterated subset?
0

There are basically two steps to be done, grouping the data by year/month and calculate the actual sum. In order to group you need a common identifier (just remove the day from your input). The sum is basically the grouping function.

$tmpArray = array_map(function($target) {
    $target['target_date'] = substr($target['target_date'], 0, 7);
    return $target;
}, $tmpArray);

$finalData = array_reduce($tmpArray, function($carry, $target) {
    if(!isset($carry[$target['target_date']])) {
        $carry[$target['target_date']] = ['bad' => 0, 'good' => 0, 'excellent' => 0];
    }
    $carry[$target['target_date']][$target['target_intensity']]++;
    return $carry;
}, []);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.