Skip to content

Instantly share code, notes, and snippets.

@kevinquinnyo
Created March 17, 2015 20:36
Show Gist options
  • Save kevinquinnyo/3a3638137ca7df755925 to your computer and use it in GitHub Desktop.
Save kevinquinnyo/3a3638137ca7df755925 to your computer and use it in GitHub Desktop.
$array = [
'0' => [
'data' => [
'traffic_in' => [
'1',
'2',
'3',
'4'
],
'traffic_out' => [
'1',
'2',
'3',
'4'
],
],
],
'1' => [
'data' => [
'traffic_in' => [
'1',
'2',
'3',
'4'
],
'traffic_out' => [
'1',
'2',
'3',
'4'
],
],
]
];
@kevinquinnyo
Copy link
Author

foreach($array as $set) {
    foreach($set['data'] as $dataSource => $values) {
        $dsArray[$dataSource][] = $values;
    }
}


$summed = [];
foreach($dsArray as $dataSourceName => $sets) { 
    if(!isset($summed[$dataSourceName])) {
        $summed[$dataSourceName] = [];
    }
    foreach($sets as $set) {
        for($i=0; $i<count($set); $i++) {
            @$summed[$dataSourceName][$i] += $set[$i];
        }
    }
}

print_r($summed);

that's really ugly but does the trick. Need a more elegant way at some point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment