Looping through an array and add up with same value of a key
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
add a comment |
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
that's ajsonstring, usejson_decodeto convert it to a php array
– subroutines
Nov 21 '18 at 5:12
add a comment |
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
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
php arrays
edited Nov 22 '18 at 7:16
alikhar2018
asked Nov 21 '18 at 5:04
alikhar2018alikhar2018
205
205
that's ajsonstring, usejson_decodeto convert it to a php array
– subroutines
Nov 21 '18 at 5:12
add a comment |
that's ajsonstring, usejson_decodeto 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
add a comment |
2 Answers
2
active
oldest
votes
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);
Glad to hear that.
– Shail Paras
Nov 21 '18 at 5:50
add a comment |
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
When I am running it on my browser with PHP version 5.6 and FPM, it gives meUndefined Indexerror.
– 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Glad to hear that.
– Shail Paras
Nov 21 '18 at 5:50
add a comment |
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);
Glad to hear that.
– Shail Paras
Nov 21 '18 at 5:50
add a comment |
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);
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);
answered Nov 21 '18 at 5:14
Shail ParasShail Paras
8421028
8421028
Glad to hear that.
– Shail Paras
Nov 21 '18 at 5:50
add a comment |
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
add a comment |
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
When I am running it on my browser with PHP version 5.6 and FPM, it gives meUndefined Indexerror.
– 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
add a comment |
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
When I am running it on my browser with PHP version 5.6 and FPM, it gives meUndefined Indexerror.
– 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
add a comment |
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
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
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 meUndefined Indexerror.
– 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
add a comment |
When I am running it on my browser with PHP version 5.6 and FPM, it gives meUndefined Indexerror.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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

that's a
jsonstring, usejson_decodeto convert it to a php array– subroutines
Nov 21 '18 at 5:12