0

I have to sum some columns of an multidimensional array, and i`m stucked for 3 days at it. My array is like this:

$items = [ 
   0 => [
     0 => [
       'category' => 'CATEGORY ONE',
       'goal' => 12,
       'reached' => '14',
       'points' => '148',
    ],
    1 => [
      'category' => 'CATEGORY TWO',
      'goal' => 12,
      'reached' => '14',
      'points' => '148',
    ]
   ],
   1 => [
     0 => [
       'category' => 'CATEGORY ONE',
       'goal' => 12,
       'reached' => '14',
       'points' => '148',
    ],
    1 => [
      'category' => 'CATEGORY TWO',
      'goal' => 12,
      'reached' => '14',
      'points' => '148',
    ]
   ]
];

i was no abble to parse this data to get the sum of values of each category so it have to return like this:

$items = [
 0 => [
   'category' => 'CATEGORY ONE',
   'goal' => 24,
   'reached' => '48',
   'points' => '296',
],
1 => [
  'category' => 'CATEGORY TWO',
  'goal' => 12,
  'reached' => '14',
  'points' => '296',
]];
2
  • How is 'reached' => '48' calculated? Commented Oct 30, 2017 at 11:40
  • 3
    Post your code so we can find mistakes and help you fix it to achieve the correct result. Commented Oct 30, 2017 at 11:47

2 Answers 2

1

This is not universal code but do just what you want

$result = [];
$categoriesData = [];
// aggregate data by category name
foreach ($items as $item) {
    foreach ($item as $category) {
        $categoryName = $category['category'];
        if (!isset($categoriesData[$categoryName])) {
            $categoriesData[$categoryName] = [
                'goal' => 0,
                'reached' => 0,
                'points' => 0
            ];
        }
        foreach ($category as $key => $value) {
            if ($key === 'category') continue;
            $categoriesData[$categoryName][$key] += (int) $value;
        }
    }
}

// result data
foreach ($categoriesData as $key => $value) {
    $result[] = [
        'category' => $key,
        'goal' => (string) $value['goal'],
        'reached' => (string) $value['reached'],
        'points' => (string) $value['points']
    ];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$itemsFlattened = call_user_func_array('array_merge', $items);

$itemsSummed = [];
foreach ($itemsFlattened as $item) {
    if (array_key_exists($item['category'], $itemsSummed)) {
        $itemsSummed[$item['category']]['goal'] += $item['goal'];
        $itemsSummed[$item['category']]['reached'] += $item['reached'];
        $itemsSummed[$item['category']]['points'] += $item['points'];
    } else {
        $itemsSummed[$item['category']] = $item;
    }
}
$itemsSummed = array_values($itemsSummed);

print_r($itemsSummed);

Output:

Array
(
    [0] => Array
        (
            [category] => CATEGORY ONE
            [goal] => 24
            [reached] => 28
            [points] => 296
        )

    [1] => Array
        (
            [category] => CATEGORY TWO
            [goal] => 24
            [reached] => 28
            [points] => 296
        )

)

2 Comments

tks dude! you saved my live!
@RichardFeliciano No problem :) If my answer helped you, don't forget to accept it

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.