Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
Colección Laravel Lumen - Group By With Sum y preservar los valores

Estoy trabajando en Lumen y mi colección tiene valores duplicados, ejemplo:

 collect([ [ 'name' => 'John Doe', 'department' => 'Sales', 'phone' => '99999-99999', 'value' => 25.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 5.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 58.0 ], [ 'name' => 'Lucas Rodrigues', 'department' => 'Marketing', 'phone' => '22222-22222', 'value' => 90.0 ] ])

Me gustaría sumar la propiedad de valor cuando el nombre es el mismo pero conservar otros valores como (departamento y teléfono) y eliminar la entidad duplicada, ejemplo:

 collect([ [ 'name' => 'John Doe', 'department' => 'Sales', 'phone' => '99999-99999', 'value' => 25.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 63.0 ], [ 'name' => 'Lucas Rodrigues', 'department' => 'Marketing', 'phone' => '22222-22222', 'value' => 90.0 ] ])

¿Cuál es la mejor manera de hacer esto?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Prueba lo siguiente:

 $newCollection = collect([]); $collectctions = collect([ [ 'name' => 'John Doe', 'department' => 'Sales', 'phone' => '99999-99999', 'value' => 25.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 5.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 58.0 ], [ 'name' => 'Lucas Rodrigues', 'department' => 'Marketing', 'phone' => '22222-22222', 'value' => 90.0 ] ]); foreach ($collectctions as $item) { if($newCollection->contains('name', $item['name'])){ $index = $newCollection->where('name', $item['name'])->keys()[0]; $newCollection = $newCollection->map(function ($object, $i) use ($index, $item) { if($i == $index){ $object['value'] += $item['value']; } return $object; }); } else{ $newCollection->push($item); } } return $newCollection;
over 4 years ago · Santiago Trujillo Report

0

Esto se puede lograr usando funciones de colección ( https://laravel.com/docs/8.x/collections ). En este ejemplo, lo resolví usando ( groupBy , map , flatten , slice , count y sum ).

 $data = collect([ [ 'name' => 'John Doe', 'department' => 'Sales', 'phone' => '99999-99999', 'value' => 25.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 5.0 ], [ 'name' => 'Mary Lisa', 'department' => 'Finance', 'phone' => '88888-88888', 'value' => 58.0 ], [ 'name' => 'Lucas Rodrigues', 'department' => 'Marketing', 'phone' => '22222-22222', 'value' => 90.0 ] ]); $data = $data->groupBy('name')->map(function ($item) { if ($item->count() > 1) { $item = $item->slice(0, 1)->map(function($subItem) use ($item) { $subItem['value'] = $item->sum('value'); return $subItem; }); } return $item; }) ->flatten(1);

Al llamar a print_r($data->toArray()); obtenemos como resultado la siguiente matriz:

 Array ( [0] => Array ( [name] => John Doe [department] => Sales [phone] => 99999-99999 [value] => 25 ) [1] => Array ( [name] => Mary Lisa [department] => Finance [phone] => 88888-88888 [value] => 63 ) [2] => Array ( [name] => Lucas Rodrigues [department] => Marketing [phone] => 22222-22222 [value] => 90 ) )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!