Group Simple Array PHP












0















Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.



This is my array:



Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)

[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)

[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)

[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)


As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:



 Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)


[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)









share|improve this question




















  • 1





    What have you tried? PS. I already solved it.

    – ArtisticPhoenix
    Jan 2 at 17:40











  • Check this - I think it the same question...

    – dWinder
    Jan 2 at 17:45








  • 3





    Possible duplicate of single multidimensional array check with other index values php

    – dWinder
    Jan 2 at 17:46











  • @DavidWinder - it's similar but the desired output is very different.

    – ArtisticPhoenix
    Jan 2 at 17:47











  • Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

    – dWinder
    Jan 2 at 18:10


















0















Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.



This is my array:



Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)

[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)

[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)

[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)


As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:



 Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)


[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)









share|improve this question




















  • 1





    What have you tried? PS. I already solved it.

    – ArtisticPhoenix
    Jan 2 at 17:40











  • Check this - I think it the same question...

    – dWinder
    Jan 2 at 17:45








  • 3





    Possible duplicate of single multidimensional array check with other index values php

    – dWinder
    Jan 2 at 17:46











  • @DavidWinder - it's similar but the desired output is very different.

    – ArtisticPhoenix
    Jan 2 at 17:47











  • Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

    – dWinder
    Jan 2 at 18:10
















0












0








0








Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.



This is my array:



Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)

[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)

[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)

[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)


As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:



 Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)


[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)









share|improve this question
















Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.



This is my array:



Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)

[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)

[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)

[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)


As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:



 Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)


[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)

[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)






php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:14







Front

















asked Jan 2 at 17:34









FrontFront

33




33








  • 1





    What have you tried? PS. I already solved it.

    – ArtisticPhoenix
    Jan 2 at 17:40











  • Check this - I think it the same question...

    – dWinder
    Jan 2 at 17:45








  • 3





    Possible duplicate of single multidimensional array check with other index values php

    – dWinder
    Jan 2 at 17:46











  • @DavidWinder - it's similar but the desired output is very different.

    – ArtisticPhoenix
    Jan 2 at 17:47











  • Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

    – dWinder
    Jan 2 at 18:10
















  • 1





    What have you tried? PS. I already solved it.

    – ArtisticPhoenix
    Jan 2 at 17:40











  • Check this - I think it the same question...

    – dWinder
    Jan 2 at 17:45








  • 3





    Possible duplicate of single multidimensional array check with other index values php

    – dWinder
    Jan 2 at 17:46











  • @DavidWinder - it's similar but the desired output is very different.

    – ArtisticPhoenix
    Jan 2 at 17:47











  • Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

    – dWinder
    Jan 2 at 18:10










1




1





What have you tried? PS. I already solved it.

– ArtisticPhoenix
Jan 2 at 17:40





What have you tried? PS. I already solved it.

– ArtisticPhoenix
Jan 2 at 17:40













Check this - I think it the same question...

– dWinder
Jan 2 at 17:45







Check this - I think it the same question...

– dWinder
Jan 2 at 17:45






3




3





Possible duplicate of single multidimensional array check with other index values php

– dWinder
Jan 2 at 17:46





Possible duplicate of single multidimensional array check with other index values php

– dWinder
Jan 2 at 17:46













@DavidWinder - it's similar but the desired output is very different.

– ArtisticPhoenix
Jan 2 at 17:47





@DavidWinder - it's similar but the desired output is very different.

– ArtisticPhoenix
Jan 2 at 17:47













Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

– dWinder
Jan 2 at 18:10







Maybe the output is different but the way to get there is the same - I would expect the OP to do that himself. And notice that some other answer there give the same output as requested here

– dWinder
Jan 2 at 18:10














1 Answer
1






active

oldest

votes


















1














You can do this with a simple foreach loop:



 $array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);

$out = ;

foreach($array as $value){

$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}

print_r($out);


Output



Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)

[38] => Array
(
[size] => 38
[quantity] => 0
)

[39] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.



 //reset the array keys
$out = array_values($out);
print_r($out);


Output



Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)

[1] => Array
(
[size] => 38
[quantity] => 0
)

[2] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



Cheers






share|improve this answer
























  • This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

    – Front
    Jan 2 at 18:52











  • Oh it was added without changing anything. Thanks!

    – Front
    Jan 2 at 18:54














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%2f54010723%2fgroup-simple-array-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can do this with a simple foreach loop:



 $array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);

$out = ;

foreach($array as $value){

$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}

print_r($out);


Output



Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)

[38] => Array
(
[size] => 38
[quantity] => 0
)

[39] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.



 //reset the array keys
$out = array_values($out);
print_r($out);


Output



Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)

[1] => Array
(
[size] => 38
[quantity] => 0
)

[2] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



Cheers






share|improve this answer
























  • This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

    – Front
    Jan 2 at 18:52











  • Oh it was added without changing anything. Thanks!

    – Front
    Jan 2 at 18:54


















1














You can do this with a simple foreach loop:



 $array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);

$out = ;

foreach($array as $value){

$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}

print_r($out);


Output



Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)

[38] => Array
(
[size] => 38
[quantity] => 0
)

[39] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.



 //reset the array keys
$out = array_values($out);
print_r($out);


Output



Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)

[1] => Array
(
[size] => 38
[quantity] => 0
)

[2] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



Cheers






share|improve this answer
























  • This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

    – Front
    Jan 2 at 18:52











  • Oh it was added without changing anything. Thanks!

    – Front
    Jan 2 at 18:54
















1












1








1







You can do this with a simple foreach loop:



 $array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);

$out = ;

foreach($array as $value){

$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}

print_r($out);


Output



Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)

[38] => Array
(
[size] => 38
[quantity] => 0
)

[39] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.



 //reset the array keys
$out = array_values($out);
print_r($out);


Output



Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)

[1] => Array
(
[size] => 38
[quantity] => 0
)

[2] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



Cheers






share|improve this answer













You can do this with a simple foreach loop:



 $array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);

$out = ;

foreach($array as $value){

$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}

print_r($out);


Output



Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)

[38] => Array
(
[size] => 38
[quantity] => 0
)

[39] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.



 //reset the array keys
$out = array_values($out);
print_r($out);


Output



Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)

[1] => Array
(
[size] => 38
[quantity] => 0
)

[2] => Array
(
[size] => 39
[quantity] => 3
)

)


Sandbox



Cheers







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:47









ArtisticPhoenixArtisticPhoenix

18.1k11226




18.1k11226













  • This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

    – Front
    Jan 2 at 18:52











  • Oh it was added without changing anything. Thanks!

    – Front
    Jan 2 at 18:54





















  • This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

    – Front
    Jan 2 at 18:52











  • Oh it was added without changing anything. Thanks!

    – Front
    Jan 2 at 18:54



















This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

– Front
Jan 2 at 18:52





This is amazing, thank you! What if I want to add another key, like my edited question. Add [sku] also..?

– Front
Jan 2 at 18:52













Oh it was added without changing anything. Thanks!

– Front
Jan 2 at 18:54







Oh it was added without changing anything. Thanks!

– Front
Jan 2 at 18:54






















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%2f54010723%2fgroup-simple-array-php%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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules