Group and add values from each group of a php array

Asked

Viewed 697 times

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 researched and tried several ways to do this, but really I’m very lost and need help with this, thanks in advance!

1 answer

2


Hello

You can use Collections https://laravel.com/docs/5.6/collections

//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');
    });

Upshot

Collection {#515 ▼
  #items: array:2 [▼
    "Tratamento Odontológico" => 5550
    "Contabilidade" => 11100
  ]
}
  • Thank you very much! It’s just that I’m new to Laravel so I didn’t know the "Collections", I just had to use the $collection down and replace the $list and $item with the variable that receives the data from the bank, then did exactly what I needed! Thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.