Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> PHP sumowanie po takim samym kluczu, Cos jak sum w MYSQL
fumfel20
post 17.11.2020, 21:37:40
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Witam, panowie mam takie dane wyjsciowe:
  1. array:5 [▼
  2. 0 => {#1565 ▼
  3. +"strProductColourName": "English Oak"
  4. +"strCenterPanelSize": "444x142x6"
  5. +"intQuantityNeeded": "1"
  6. }
  7. 1 => {#1563 ▼
  8. +"strProductColourName": "English Oak"
  9. +"strCenterPanelSize": "444x142x6"
  10. +"intQuantityNeeded": "4"
  11. }
  12. 2 => {#1561 ▼
  13. +"strProductColourName": "English Oak"
  14. +"strCenterPanelSize": "748x370.5x6"
  15. +"intQuantityNeeded": "2"
  16. }
  17. 3 => {#1560 ▼
  18. +"strProductColourName": "English Oak"
  19. +"strCenterPanelSize": "900x256x6"
  20. +"intQuantityNeeded": "2"
  21. }
  22. 4 => {#1559 ▼
  23. +"strProductColourName": "English Oak"
  24. +"strCenterPanelSize": "900x256x6"
  25. +"intQuantityNeeded": "2"
  26. }
  27. ]


potrzebuje zsumowac intQuantityNeeded po strProductColourName (bo kolor moze byc inny a wtedy nie moge juz sumowac) oraz strCenterPanelSize:
  1. array:5 [▼
  2. 0 => {#1565 ▼
  3. +"strProductColourName": "English Oak"
  4. +"strCenterPanelSize": "444x142x6"
  5. +"intQuantityNeeded": "5"
  6. }
  7. 1 => {#1561 ▼
  8. +"strProductColourName": "English Oak"
  9. +"strCenterPanelSize": "748x370.5x6"
  10. +"intQuantityNeeded": "2"
  11. }
  12. 2 => {#1560 ▼
  13. +"strProductColourName": "English Oak"
  14. +"strCenterPanelSize": "900x256x6"
  15. +"intQuantityNeeded": "4"
  16. }
  17.  
  18. ]


Ma ktos jakis pomysl? Z gory dzieki bo juz nie mam pomyslu.
Go to the top of the page
+Quote Post
Smoker
post 19.11.2020, 21:20:19
Post #2





Grupa: Zarejestrowani
Postów: 101
Pomógł: 5
Dołączył: 15.10.2008
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Nie wiem w jakim języku masz te dane wejściowe, ale pytanie zadajesz w dziale PHP więc finalnie masz je chyba w zwykłej tablicy? Jeżeli tak, to lecisz po niej i sumujesz co potrzeba, poniżej przykład i wyniki (nie wiem czemu liczby masz podane jako string ale niech i tak będzie):

  1. <?php
  2. $input = [
  3. 0 => [
  4. "strProductColourName" => "English Oak",
  5. "strCenterPanelSize" => "444x142x6",
  6. "intQuantityNeeded" => "1"
  7. ],
  8. 1 => [
  9. "strProductColourName" => "English Oak",
  10. "strCenterPanelSize" => "444x142x6",
  11. "intQuantityNeeded" => "4"
  12. ],
  13. 2 => [
  14. "strProductColourName" => "English Oak",
  15. "strCenterPanelSize" => "748x370.5x6",
  16. "intQuantityNeeded" => "2"
  17. ],
  18. 3 => [
  19. "strProductColourName" => "English Oak",
  20. "strCenterPanelSize" => "900x256x6",
  21. "intQuantityNeeded" => "2"
  22. ],
  23. 4 => [
  24. "strProductColourName" => "English Oak",
  25. "strCenterPanelSize" => "900x256x6",
  26. "intQuantityNeeded" => "2"
  27. ]
  28. ];
  29.  
  30. foreach($input as $value){
  31. $sumaColor[$value['strProductColourName']] = isSet($sumaColor[$value['strProductColourName']]) ? $sumaColor[$value['strProductColourName']] + $value['intQuantityNeeded'] : $value['intQuantityNeeded'];
  32. $sumaPanel[$value['strCenterPanelSize']] = isSet($sumaPanel[$value['strCenterPanelSize']]) ? $sumaPanel[$value['strCenterPanelSize']] + $value['intQuantityNeeded'] : $value['intQuantityNeeded'];
  33. }
  34.  
  35. var_dump($sumaColor);
  36. /**
  37. * array (size=1)
  38. * 'English Oak' => int 11
  39. */
  40.  
  41. var_dump($sumaPanel);
  42. /**
  43. * array (size=3)
  44. * '444x142x6' => int 5
  45. * '748x370.5x6' => string '2' (length=1)
  46. * '900x256x6' => int 4
  47. */
  48. ?>


Ten post edytował Smoker 19.11.2020, 21:21:14


--------------------
http://streetonline.pl/?gback=1 - Kliknij i daj się podwieźć
Go to the top of the page
+Quote Post
SmokAnalog
post 19.11.2020, 21:48:41
Post #3





Grupa: Zarejestrowani
Postów: 1 707
Pomógł: 266
Dołączył: 3.07.2012
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Ja bym pojechał z array_reduce:

  1. $result = array_reduce(
  2. $data,
  3. function (array $carry, array $item): array {
  4. $matchingItemIndex = array_key_first(array_filter($carry, fn(array $groupedItem): bool => (
  5. $groupedItem['strProductColourName'] === $item['strProductColourName']
  6. && $groupedItem['strCenterPanelSize'] === $item['strCenterPanelSize']
  7. )));
  8.  
  9. if ($matchingItemIndex !== null) {
  10. $carry[$matchingItemIndex]['intQuantityNeeded'] += $item['intQuantityNeeded'];
  11. } else {
  12. $carry[] = $item;
  13. }
  14.  
  15. return $carry;
  16. },
  17. []
  18. );
Go to the top of the page
+Quote Post
fumfel20
post 8.12.2020, 21:24:17
Post #4





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Dzieki Smok. Bardzo mi pomogles.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 17:45