A
A
AlmeryCloud2019-10-06 13:30:11
MySQL
AlmeryCloud, 2019-10-06 13:30:11

How to sum values ​​with same names?

5d99c08a0d0ec835080649.png
By referring to MySQL with a specific query, I get an array with the values ​​​​of different tables. Can I change my query so that instead of two repeating array elements with the name "Food and Drinks" I get one element, where the amount of elements with the repeating name will be summed up? It turns out I want the array to have an element with the name "Food and Drinks", where amount = "700".

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arthur, 2019-10-06
@AlmeryCloud

Group by name and sum(amount)

V
Vladimir Kokhan, 2019-10-06
@SkazochNick

Collection->pluck('name')->sum()

G
Grigory Vasilkov, 2019-10-06
@gzhegow

You can also call array_combine([id,name], array_map(null, $result)) and instead get

[
  0 => [ id => 1, name => 'vasya' ],
  1 => [ id => 2, name => 'petya' ],
]

array
[
  id => [ 1,2 ],
  name => [ vasya, petya ],
]

summarize later, filter what you want to do

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question