Looping through an array and add up with same value of a key












2















This is the sample json encoded array-



[
{"item_id":"8057","category":"MEN'S CLOTHING","quantity":"3.000"},
{"item_id":"22647","category":"WOMEN'S CLOTHING","quantity":"7.000"},
{"item_id":"1556","category":"MEN'S CLOTHING","quantity":"2.000"},
{"item_id":"4179","category":"WOMEN'S CLOTHING","quantity":"1.000"},
{"item_id":"21218","category":"WOMEN'S CLOTHING","quantity":"2.000"}
]


I want to add up quantity with same category.



Need final result something like-



"MEN'S CLOTHING" : 5,
"WOMEN'S CLOTHING": 10


Note: the value of key 'category' is dynamic










share|improve this question

























  • that's a json string, use json_decode to convert it to a php array

    – subroutines
    Nov 21 '18 at 5:12
















2















This is the sample json encoded array-



[
{"item_id":"8057","category":"MEN'S CLOTHING","quantity":"3.000"},
{"item_id":"22647","category":"WOMEN'S CLOTHING","quantity":"7.000"},
{"item_id":"1556","category":"MEN'S CLOTHING","quantity":"2.000"},
{"item_id":"4179","category":"WOMEN'S CLOTHING","quantity":"1.000"},
{"item_id":"21218","category":"WOMEN'S CLOTHING","quantity":"2.000"}
]


I want to add up quantity with same category.



Need final result something like-



"MEN'S CLOTHING" : 5,
"WOMEN'S CLOTHING": 10


Note: the value of key 'category' is dynamic










share|improve this question

























  • that's a json string, use json_decode to convert it to a php array

    – subroutines
    Nov 21 '18 at 5:12














2












2








2


0






This is the sample json encoded array-



[
{"item_id":"8057","category":"MEN'S CLOTHING","quantity":"3.000"},
{"item_id":"22647","category":"WOMEN'S CLOTHING","quantity":"7.000"},
{"item_id":"1556","category":"MEN'S CLOTHING","quantity":"2.000"},
{"item_id":"4179","category":"WOMEN'S CLOTHING","quantity":"1.000"},
{"item_id":"21218","category":"WOMEN'S CLOTHING","quantity":"2.000"}
]


I want to add up quantity with same category.



Need final result something like-



"MEN'S CLOTHING" : 5,
"WOMEN'S CLOTHING": 10


Note: the value of key 'category' is dynamic










share|improve this question
















This is the sample json encoded array-



[
{"item_id":"8057","category":"MEN'S CLOTHING","quantity":"3.000"},
{"item_id":"22647","category":"WOMEN'S CLOTHING","quantity":"7.000"},
{"item_id":"1556","category":"MEN'S CLOTHING","quantity":"2.000"},
{"item_id":"4179","category":"WOMEN'S CLOTHING","quantity":"1.000"},
{"item_id":"21218","category":"WOMEN'S CLOTHING","quantity":"2.000"}
]


I want to add up quantity with same category.



Need final result something like-



"MEN'S CLOTHING" : 5,
"WOMEN'S CLOTHING": 10


Note: the value of key 'category' is dynamic







php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:16







alikhar2018

















asked Nov 21 '18 at 5:04









alikhar2018alikhar2018

205




205













  • that's a json string, use json_decode to convert it to a php array

    – subroutines
    Nov 21 '18 at 5:12



















  • that's a json string, use json_decode to convert it to a php array

    – subroutines
    Nov 21 '18 at 5:12

















that's a json string, use json_decode to convert it to a php array

– subroutines
Nov 21 '18 at 5:12





that's a json string, use json_decode to convert it to a php array

– subroutines
Nov 21 '18 at 5:12












2 Answers
2






active

oldest

votes


















1














You can try something like that:



$dataArray = json_decode($jsonArray, true);
$allValuesWithCount = array();
foreach($dataArray as $arrayValues) {
$allValuesWithCount[$arrayValues['category']] += $arrayValues['quantity'];
}
print_r($allValuesWithCount);





share|improve this answer
























  • Glad to hear that.

    – Shail Paras
    Nov 21 '18 at 5:50



















0














You can achieve this using below code:



    $json = json_decode($string,true);

$allCount = ;
foreach ($json as $key => $value){
$allCount[$value['category']] += $value['quantity'];
}
foreach ($allCount as $category => $value){
echo $category ." : ".$value."<br>";
}


But When I run it in different PHP version through the console, it gives the warnings and displayed following result:




PHP Notice: Undefined index: MEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0



PHP Notice: Undefined index: WOMEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0




MEN'S CLOTHING : 5
WOMEN'S CLOTHING : 10





share|improve this answer


























  • When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

    – akshaypjoshi
    Nov 21 '18 at 5:57











  • Also I have tried different PHP versions, it was just warning for both categories.

    – akshaypjoshi
    Nov 21 '18 at 6:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405556%2flooping-through-an-array-and-add-up-with-same-value-of-a-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can try something like that:



$dataArray = json_decode($jsonArray, true);
$allValuesWithCount = array();
foreach($dataArray as $arrayValues) {
$allValuesWithCount[$arrayValues['category']] += $arrayValues['quantity'];
}
print_r($allValuesWithCount);





share|improve this answer
























  • Glad to hear that.

    – Shail Paras
    Nov 21 '18 at 5:50
















1














You can try something like that:



$dataArray = json_decode($jsonArray, true);
$allValuesWithCount = array();
foreach($dataArray as $arrayValues) {
$allValuesWithCount[$arrayValues['category']] += $arrayValues['quantity'];
}
print_r($allValuesWithCount);





share|improve this answer
























  • Glad to hear that.

    – Shail Paras
    Nov 21 '18 at 5:50














1












1








1







You can try something like that:



$dataArray = json_decode($jsonArray, true);
$allValuesWithCount = array();
foreach($dataArray as $arrayValues) {
$allValuesWithCount[$arrayValues['category']] += $arrayValues['quantity'];
}
print_r($allValuesWithCount);





share|improve this answer













You can try something like that:



$dataArray = json_decode($jsonArray, true);
$allValuesWithCount = array();
foreach($dataArray as $arrayValues) {
$allValuesWithCount[$arrayValues['category']] += $arrayValues['quantity'];
}
print_r($allValuesWithCount);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 5:14









Shail ParasShail Paras

8421028




8421028













  • Glad to hear that.

    – Shail Paras
    Nov 21 '18 at 5:50



















  • Glad to hear that.

    – Shail Paras
    Nov 21 '18 at 5:50

















Glad to hear that.

– Shail Paras
Nov 21 '18 at 5:50





Glad to hear that.

– Shail Paras
Nov 21 '18 at 5:50













0














You can achieve this using below code:



    $json = json_decode($string,true);

$allCount = ;
foreach ($json as $key => $value){
$allCount[$value['category']] += $value['quantity'];
}
foreach ($allCount as $category => $value){
echo $category ." : ".$value."<br>";
}


But When I run it in different PHP version through the console, it gives the warnings and displayed following result:




PHP Notice: Undefined index: MEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0



PHP Notice: Undefined index: WOMEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0




MEN'S CLOTHING : 5
WOMEN'S CLOTHING : 10





share|improve this answer


























  • When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

    – akshaypjoshi
    Nov 21 '18 at 5:57











  • Also I have tried different PHP versions, it was just warning for both categories.

    – akshaypjoshi
    Nov 21 '18 at 6:14
















0














You can achieve this using below code:



    $json = json_decode($string,true);

$allCount = ;
foreach ($json as $key => $value){
$allCount[$value['category']] += $value['quantity'];
}
foreach ($allCount as $category => $value){
echo $category ." : ".$value."<br>";
}


But When I run it in different PHP version through the console, it gives the warnings and displayed following result:




PHP Notice: Undefined index: MEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0



PHP Notice: Undefined index: WOMEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0




MEN'S CLOTHING : 5
WOMEN'S CLOTHING : 10





share|improve this answer


























  • When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

    – akshaypjoshi
    Nov 21 '18 at 5:57











  • Also I have tried different PHP versions, it was just warning for both categories.

    – akshaypjoshi
    Nov 21 '18 at 6:14














0












0








0







You can achieve this using below code:



    $json = json_decode($string,true);

$allCount = ;
foreach ($json as $key => $value){
$allCount[$value['category']] += $value['quantity'];
}
foreach ($allCount as $category => $value){
echo $category ." : ".$value."<br>";
}


But When I run it in different PHP version through the console, it gives the warnings and displayed following result:




PHP Notice: Undefined index: MEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0



PHP Notice: Undefined index: WOMEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0




MEN'S CLOTHING : 5
WOMEN'S CLOTHING : 10





share|improve this answer















You can achieve this using below code:



    $json = json_decode($string,true);

$allCount = ;
foreach ($json as $key => $value){
$allCount[$value['category']] += $value['quantity'];
}
foreach ($allCount as $category => $value){
echo $category ." : ".$value."<br>";
}


But When I run it in different PHP version through the console, it gives the warnings and displayed following result:




PHP Notice: Undefined index: MEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0



PHP Notice: Undefined index: WOMEN'S CLOTHING in /home/akshay/so.php on line 15
PHP Stack trace:
PHP 1. {main}() /home/akshay/so.php:0




MEN'S CLOTHING : 5
WOMEN'S CLOTHING : 10






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 6:20

























answered Nov 21 '18 at 5:43









akshaypjoshiakshaypjoshi

5661316




5661316













  • When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

    – akshaypjoshi
    Nov 21 '18 at 5:57











  • Also I have tried different PHP versions, it was just warning for both categories.

    – akshaypjoshi
    Nov 21 '18 at 6:14



















  • When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

    – akshaypjoshi
    Nov 21 '18 at 5:57











  • Also I have tried different PHP versions, it was just warning for both categories.

    – akshaypjoshi
    Nov 21 '18 at 6:14

















When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

– akshaypjoshi
Nov 21 '18 at 5:57





When I am running it on my browser with PHP version 5.6 and FPM, it gives me Undefined Index error.

– akshaypjoshi
Nov 21 '18 at 5:57













Also I have tried different PHP versions, it was just warning for both categories.

– akshaypjoshi
Nov 21 '18 at 6:14





Also I have tried different PHP versions, it was just warning for both categories.

– akshaypjoshi
Nov 21 '18 at 6:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405556%2flooping-through-an-array-and-add-up-with-same-value-of-a-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith