PHP array: Conditional sum
I have a an array looking like this:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
I would like to sum values foreach "unique" key when only the first character is considered. The result should then be:
$newarray = array("a" => 3, "b" => 5);
I have tried using a foreach() loop within another foreach() loop like this:
foreach ($xml->children() as $output) {
foreach ($array as $key => $value) {
if (substr($key,0,1) == $output->KEY) {
$sum += $value; echo $sum;
}
}
}
It didn't work as the results apparently added the prior calculations.
php arrays calculation
add a comment |
I have a an array looking like this:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
I would like to sum values foreach "unique" key when only the first character is considered. The result should then be:
$newarray = array("a" => 3, "b" => 5);
I have tried using a foreach() loop within another foreach() loop like this:
foreach ($xml->children() as $output) {
foreach ($array as $key => $value) {
if (substr($key,0,1) == $output->KEY) {
$sum += $value; echo $sum;
}
}
}
It didn't work as the results apparently added the prior calculations.
php arrays calculation
add a comment |
I have a an array looking like this:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
I would like to sum values foreach "unique" key when only the first character is considered. The result should then be:
$newarray = array("a" => 3, "b" => 5);
I have tried using a foreach() loop within another foreach() loop like this:
foreach ($xml->children() as $output) {
foreach ($array as $key => $value) {
if (substr($key,0,1) == $output->KEY) {
$sum += $value; echo $sum;
}
}
}
It didn't work as the results apparently added the prior calculations.
php arrays calculation
I have a an array looking like this:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
I would like to sum values foreach "unique" key when only the first character is considered. The result should then be:
$newarray = array("a" => 3, "b" => 5);
I have tried using a foreach() loop within another foreach() loop like this:
foreach ($xml->children() as $output) {
foreach ($array as $key => $value) {
if (substr($key,0,1) == $output->KEY) {
$sum += $value; echo $sum;
}
}
}
It didn't work as the results apparently added the prior calculations.
php arrays calculation
php arrays calculation
edited Jan 3 at 2:40
Alive to Die
56k83273
56k83273
asked Jan 2 at 13:52
McClaudLiveMcClaudLive
186
186
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can try this one. It will take first letter from your key and make sum of all values with that first letter keys.
<?php
$sum = ;
foreach ($array as $key => $value) {
$letter = substr($key,0,1);
if (!isset($sum[$letter])){$sum[$letter] = 0;}
$sum[$letter] += $value;
}
var_dump($sum);
1
@executable only the first character needs to be considered for OP. Even if$key = 'ab';
this would result in$letter = 'a';
sincesubstr($key, 0, 1)
return the first character
– G4Hu
Jan 2 at 14:10
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
add a comment |
Simple solution:
$final_array=;
foreach($array as $key=>$value){
$final_array[$key[0]] = (isset($final_array[$key[0]])) ? $final_array[$key[0]]+$value : $value;
}
print_r($final_array);
Output:- https://3v4l.org/tZ4Ei
add a comment |
A simple isset
should do it:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
$result = array();
foreach ($array as $oldkey => $val) {
$newkey = substr($oldkey, 0, 1);
if (isset($result[$newkey]) === false)
$result[$newkey] = $val;
else
$result[$newkey] += $val;
}
var_dump($result);
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
add a comment |
Try this way
$array = array('a1' => 0, 'a2' => 2, 'a3' => 3, 'b1' => 2, 'b2' => 3);
$result = array();
foreach($array as $key => $value){
if(isset($result[$key[0]])){
$result[$key[0]] = $result[$key[0]]+$value;
} else {
$result[$key[0]] = $value;
}
}
print_r($result);
1
You have a missing)
in your condition
– executable
Jan 2 at 14:03
add a comment |
Quick and Easy, you can have any number of numbers in the array after the characters.
<?php
$array = ["a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3];
$newArray = ;
foreach ($array as $key => $value) {
$key = preg_replace("/[0-9]*$/", "", $key);
$newArray[$key] = (isset($newArray[$key])) ? $newArray[$key] + $value : $value;
}
print_r($newArray);
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%2f54007565%2fphp-array-conditional-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try this one. It will take first letter from your key and make sum of all values with that first letter keys.
<?php
$sum = ;
foreach ($array as $key => $value) {
$letter = substr($key,0,1);
if (!isset($sum[$letter])){$sum[$letter] = 0;}
$sum[$letter] += $value;
}
var_dump($sum);
1
@executable only the first character needs to be considered for OP. Even if$key = 'ab';
this would result in$letter = 'a';
sincesubstr($key, 0, 1)
return the first character
– G4Hu
Jan 2 at 14:10
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
add a comment |
You can try this one. It will take first letter from your key and make sum of all values with that first letter keys.
<?php
$sum = ;
foreach ($array as $key => $value) {
$letter = substr($key,0,1);
if (!isset($sum[$letter])){$sum[$letter] = 0;}
$sum[$letter] += $value;
}
var_dump($sum);
1
@executable only the first character needs to be considered for OP. Even if$key = 'ab';
this would result in$letter = 'a';
sincesubstr($key, 0, 1)
return the first character
– G4Hu
Jan 2 at 14:10
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
add a comment |
You can try this one. It will take first letter from your key and make sum of all values with that first letter keys.
<?php
$sum = ;
foreach ($array as $key => $value) {
$letter = substr($key,0,1);
if (!isset($sum[$letter])){$sum[$letter] = 0;}
$sum[$letter] += $value;
}
var_dump($sum);
You can try this one. It will take first letter from your key and make sum of all values with that first letter keys.
<?php
$sum = ;
foreach ($array as $key => $value) {
$letter = substr($key,0,1);
if (!isset($sum[$letter])){$sum[$letter] = 0;}
$sum[$letter] += $value;
}
var_dump($sum);
answered Jan 2 at 13:58
JohnnyB1988JohnnyB1988
1467
1467
1
@executable only the first character needs to be considered for OP. Even if$key = 'ab';
this would result in$letter = 'a';
sincesubstr($key, 0, 1)
return the first character
– G4Hu
Jan 2 at 14:10
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
add a comment |
1
@executable only the first character needs to be considered for OP. Even if$key = 'ab';
this would result in$letter = 'a';
sincesubstr($key, 0, 1)
return the first character
– G4Hu
Jan 2 at 14:10
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
1
1
@executable only the first character needs to be considered for OP. Even if
$key = 'ab';
this would result in $letter = 'a';
since substr($key, 0, 1)
return the first character– G4Hu
Jan 2 at 14:10
@executable only the first character needs to be considered for OP. Even if
$key = 'ab';
this would result in $letter = 'a';
since substr($key, 0, 1)
return the first character– G4Hu
Jan 2 at 14:10
2
2
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
Author asked just with first character " sum values foreach "unique" key when only the first character is considered" You can also validate key with ctype_alpha to check if it is letter, or number and by that result make additional decisions
– JohnnyB1988
Jan 2 at 14:10
add a comment |
Simple solution:
$final_array=;
foreach($array as $key=>$value){
$final_array[$key[0]] = (isset($final_array[$key[0]])) ? $final_array[$key[0]]+$value : $value;
}
print_r($final_array);
Output:- https://3v4l.org/tZ4Ei
add a comment |
Simple solution:
$final_array=;
foreach($array as $key=>$value){
$final_array[$key[0]] = (isset($final_array[$key[0]])) ? $final_array[$key[0]]+$value : $value;
}
print_r($final_array);
Output:- https://3v4l.org/tZ4Ei
add a comment |
Simple solution:
$final_array=;
foreach($array as $key=>$value){
$final_array[$key[0]] = (isset($final_array[$key[0]])) ? $final_array[$key[0]]+$value : $value;
}
print_r($final_array);
Output:- https://3v4l.org/tZ4Ei
Simple solution:
$final_array=;
foreach($array as $key=>$value){
$final_array[$key[0]] = (isset($final_array[$key[0]])) ? $final_array[$key[0]]+$value : $value;
}
print_r($final_array);
Output:- https://3v4l.org/tZ4Ei
edited Jan 3 at 8:29
answered Jan 2 at 14:11
Alive to DieAlive to Die
56k83273
56k83273
add a comment |
add a comment |
A simple isset
should do it:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
$result = array();
foreach ($array as $oldkey => $val) {
$newkey = substr($oldkey, 0, 1);
if (isset($result[$newkey]) === false)
$result[$newkey] = $val;
else
$result[$newkey] += $val;
}
var_dump($result);
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
add a comment |
A simple isset
should do it:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
$result = array();
foreach ($array as $oldkey => $val) {
$newkey = substr($oldkey, 0, 1);
if (isset($result[$newkey]) === false)
$result[$newkey] = $val;
else
$result[$newkey] += $val;
}
var_dump($result);
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
add a comment |
A simple isset
should do it:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
$result = array();
foreach ($array as $oldkey => $val) {
$newkey = substr($oldkey, 0, 1);
if (isset($result[$newkey]) === false)
$result[$newkey] = $val;
else
$result[$newkey] += $val;
}
var_dump($result);
A simple isset
should do it:
$array = array("a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3);
$result = array();
foreach ($array as $oldkey => $val) {
$newkey = substr($oldkey, 0, 1);
if (isset($result[$newkey]) === false)
$result[$newkey] = $val;
else
$result[$newkey] += $val;
}
var_dump($result);
edited Jan 2 at 14:00
answered Jan 2 at 13:58
Salman ASalman A
184k67343441
184k67343441
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
add a comment |
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
Damn it :D Just few seconds later
– JohnnyB1988
Jan 2 at 13:59
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@JohnnyB1988 To be precise two seconds later.
– CodeIt
Jan 2 at 14:18
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
@Salman I'm glad that my code is like yours, it means that my studies is on good way.
– JohnnyB1988
Jan 2 at 14:21
add a comment |
Try this way
$array = array('a1' => 0, 'a2' => 2, 'a3' => 3, 'b1' => 2, 'b2' => 3);
$result = array();
foreach($array as $key => $value){
if(isset($result[$key[0]])){
$result[$key[0]] = $result[$key[0]]+$value;
} else {
$result[$key[0]] = $value;
}
}
print_r($result);
1
You have a missing)
in your condition
– executable
Jan 2 at 14:03
add a comment |
Try this way
$array = array('a1' => 0, 'a2' => 2, 'a3' => 3, 'b1' => 2, 'b2' => 3);
$result = array();
foreach($array as $key => $value){
if(isset($result[$key[0]])){
$result[$key[0]] = $result[$key[0]]+$value;
} else {
$result[$key[0]] = $value;
}
}
print_r($result);
1
You have a missing)
in your condition
– executable
Jan 2 at 14:03
add a comment |
Try this way
$array = array('a1' => 0, 'a2' => 2, 'a3' => 3, 'b1' => 2, 'b2' => 3);
$result = array();
foreach($array as $key => $value){
if(isset($result[$key[0]])){
$result[$key[0]] = $result[$key[0]]+$value;
} else {
$result[$key[0]] = $value;
}
}
print_r($result);
Try this way
$array = array('a1' => 0, 'a2' => 2, 'a3' => 3, 'b1' => 2, 'b2' => 3);
$result = array();
foreach($array as $key => $value){
if(isset($result[$key[0]])){
$result[$key[0]] = $result[$key[0]]+$value;
} else {
$result[$key[0]] = $value;
}
}
print_r($result);
edited Jan 2 at 14:46
executable
1,9452924
1,9452924
answered Jan 2 at 14:00
TamimTamim
320511
320511
1
You have a missing)
in your condition
– executable
Jan 2 at 14:03
add a comment |
1
You have a missing)
in your condition
– executable
Jan 2 at 14:03
1
1
You have a missing
)
in your condition– executable
Jan 2 at 14:03
You have a missing
)
in your condition– executable
Jan 2 at 14:03
add a comment |
Quick and Easy, you can have any number of numbers in the array after the characters.
<?php
$array = ["a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3];
$newArray = ;
foreach ($array as $key => $value) {
$key = preg_replace("/[0-9]*$/", "", $key);
$newArray[$key] = (isset($newArray[$key])) ? $newArray[$key] + $value : $value;
}
print_r($newArray);
add a comment |
Quick and Easy, you can have any number of numbers in the array after the characters.
<?php
$array = ["a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3];
$newArray = ;
foreach ($array as $key => $value) {
$key = preg_replace("/[0-9]*$/", "", $key);
$newArray[$key] = (isset($newArray[$key])) ? $newArray[$key] + $value : $value;
}
print_r($newArray);
add a comment |
Quick and Easy, you can have any number of numbers in the array after the characters.
<?php
$array = ["a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3];
$newArray = ;
foreach ($array as $key => $value) {
$key = preg_replace("/[0-9]*$/", "", $key);
$newArray[$key] = (isset($newArray[$key])) ? $newArray[$key] + $value : $value;
}
print_r($newArray);
Quick and Easy, you can have any number of numbers in the array after the characters.
<?php
$array = ["a1" => 0, "a2" => 2, "a3" => 1, "b1" => 2, "b2" => 3];
$newArray = ;
foreach ($array as $key => $value) {
$key = preg_replace("/[0-9]*$/", "", $key);
$newArray[$key] = (isset($newArray[$key])) ? $newArray[$key] + $value : $value;
}
print_r($newArray);
answered Jan 2 at 14:03
AnugaAnuga
1,069719
1,069719
add a comment |
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%2f54007565%2fphp-array-conditional-sum%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