Group and sum values from each group of a php laravel array

0

I have the following array:

  array(9) {
    [0]=>
    object(stdClass)#2477 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Tratamento Odontológico"
      ["value"]=>
      string(15) "333.33333333333"
    }
    [1]=>
    object(stdClass)#2478 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Tratamento Odontológico"
      ["value"]=>
      string(15) "333.33333333333"
    }
    [2]=>
    object(stdClass)#2479 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Tratamento Odontológico"
      ["value"]=>
      string(15) "333.33333333333"
    }
    [3]=>
    object(stdClass)#2480 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Tratamento Odontológico"
      ["value"]=>
      string(15) "333.33333333333"
    }
    [4]=>
    object(stdClass)#2481 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Tratamento Odontológico"
      ["value"]=>
      string(15) "333.33333333333"
    }
    [5]=>
    object(stdClass)#2482 (4) {
      ["type"]=>
      string(2) "out"
      ["category"]=>
      string(24) "Contabilidade"
      ["value"]=>
      string(15) "85.106382978723"
    }
    [6]=>
    object(stdClass)#2483 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Celular"
      ["value"]=>
      string(15) "100"
    }
    [7]=>
    object(stdClass)#2484 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Celular"
      ["value"]=>
      string(15) "100"
    }
    [8]=>
    object(stdClass)#2485 (4) {
      ["type"]=>
      string(2) "in"
      ["category"]=>
      string(24) "Celular"
      ["value"]=>
      string(15) "100"
    }
}

I need to group the category field so that only 1 item of each different category type is returned and add the values of each category. This needs to return, for example:

array(3) {
  [0]=>
  object(stdClass)#2477 (4) {
    ["type"]=>
    string(2) "in"
    ["category"]=>
    string(24) "Tratamento Odontológico"
    ["value"]=>
    string(16) "1666,666666667"
  }
  [1]=>
  object(stdClass)#2478 (4) {
    ["type"]=>
    string(2) "out"
    ["category"]=>
    string(24) "Contabilidade"
    ["value"]=>
    string(15) "85.106382978723"
  }
  [2]=>
  object(stdClass)#2479 (4) {
    ["type"]=>
    string(2) "in"
    ["category"]=>
    string(24) "Celular"
    ["value"]=>
    string(15) "300"
  }
}

I've already researched and tried several ways to do this, but I'm really very lost and need help with this, thank you in advance!

    
asked by anonymous 12.07.2018 / 21:00

1 answer

1

Hello

You can use collections link

//simular valores
$lista = []; 

    for ($i = 0; $i < 100; $i++) {
        $item = new stdClass();
        if ($i % 2 == 0) {
            $item->category = "Tratamento Odontológico";
            $item->value = 111;
        } else {
            $item->category = "Contabilidade";
            $item->value = 222;
        }

        $lista[] = $item;
    }


    //criar uma Collection com base no array, agrupar por category numa nova Collection, criar uma nova Collection com os items agrupados por categoria com o valor resultante da soma desses items.

    $colecao = collect($lista)->groupBy(function ($item, $key) {
        return $item->category;
    })->map(function ($item, $key) {
        return $item->sum('value');
    });

Result

Collection {#515 ▼
  #items: array:2 [▼
    "Tratamento Odontológico" => 5550
    "Contabilidade" => 11100
  ]
}
    
12.07.2018 / 23:53