How to get total of 2 arrays












-1















How do I get the total of 2 arrays.



$array1 = [176, 0];
$array2 = [0, 160];

$combinedArrays = $array1 + $array2;

// Actual result
[176, 0]

// Desired result
[176, 160]









share|improve this question

























  • What's the desired output?

    – Camilo
    Nov 14 '18 at 3:35











  • To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

    – Camilo
    Nov 14 '18 at 3:51











  • @Camilo thanks for all the help. What is your suggested question then?

    – Jearson
    Nov 14 '18 at 3:59
















-1















How do I get the total of 2 arrays.



$array1 = [176, 0];
$array2 = [0, 160];

$combinedArrays = $array1 + $array2;

// Actual result
[176, 0]

// Desired result
[176, 160]









share|improve this question

























  • What's the desired output?

    – Camilo
    Nov 14 '18 at 3:35











  • To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

    – Camilo
    Nov 14 '18 at 3:51











  • @Camilo thanks for all the help. What is your suggested question then?

    – Jearson
    Nov 14 '18 at 3:59














-1












-1








-1








How do I get the total of 2 arrays.



$array1 = [176, 0];
$array2 = [0, 160];

$combinedArrays = $array1 + $array2;

// Actual result
[176, 0]

// Desired result
[176, 160]









share|improve this question
















How do I get the total of 2 arrays.



$array1 = [176, 0];
$array2 = [0, 160];

$combinedArrays = $array1 + $array2;

// Actual result
[176, 0]

// Desired result
[176, 160]






php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 3:43









Camilo

2,0051427




2,0051427










asked Nov 14 '18 at 3:31









JearsonJearson

76113




76113













  • What's the desired output?

    – Camilo
    Nov 14 '18 at 3:35











  • To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

    – Camilo
    Nov 14 '18 at 3:51











  • @Camilo thanks for all the help. What is your suggested question then?

    – Jearson
    Nov 14 '18 at 3:59



















  • What's the desired output?

    – Camilo
    Nov 14 '18 at 3:35











  • To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

    – Camilo
    Nov 14 '18 at 3:51











  • @Camilo thanks for all the help. What is your suggested question then?

    – Jearson
    Nov 14 '18 at 3:59

















What's the desired output?

– Camilo
Nov 14 '18 at 3:35





What's the desired output?

– Camilo
Nov 14 '18 at 3:35













To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

– Camilo
Nov 14 '18 at 3:51





To make your question more helpful to others, it would be nice if you try to improve it a little bit, even if it has been answered already.

– Camilo
Nov 14 '18 at 3:51













@Camilo thanks for all the help. What is your suggested question then?

– Jearson
Nov 14 '18 at 3:59





@Camilo thanks for all the help. What is your suggested question then?

– Jearson
Nov 14 '18 at 3:59












1 Answer
1






active

oldest

votes


















4














You can do something like this



$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);


array_filter will take the 0 out and array_merge will merge 2 arrays together.



You can use



$array3 = array_values(array_filter(array_merge($array1,$array2)));


to reset the keys.



PHP refer for array_filter and array_merge






share|improve this answer


























  • Thanks. But it will not preserve the keys if I will merge the two arrays.

    – Jearson
    Nov 14 '18 at 3:40













  • use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

    – Long Kim
    Nov 14 '18 at 3:43











  • The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

    – Camilo
    Nov 14 '18 at 3:47











  • @camilo editted

    – Long Kim
    Nov 14 '18 at 3:48











  • @LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

    – Jearson
    Nov 14 '18 at 6:53













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%2f53292797%2fhow-to-get-total-of-2-arrays%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









4














You can do something like this



$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);


array_filter will take the 0 out and array_merge will merge 2 arrays together.



You can use



$array3 = array_values(array_filter(array_merge($array1,$array2)));


to reset the keys.



PHP refer for array_filter and array_merge






share|improve this answer


























  • Thanks. But it will not preserve the keys if I will merge the two arrays.

    – Jearson
    Nov 14 '18 at 3:40













  • use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

    – Long Kim
    Nov 14 '18 at 3:43











  • The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

    – Camilo
    Nov 14 '18 at 3:47











  • @camilo editted

    – Long Kim
    Nov 14 '18 at 3:48











  • @LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

    – Jearson
    Nov 14 '18 at 6:53


















4














You can do something like this



$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);


array_filter will take the 0 out and array_merge will merge 2 arrays together.



You can use



$array3 = array_values(array_filter(array_merge($array1,$array2)));


to reset the keys.



PHP refer for array_filter and array_merge






share|improve this answer


























  • Thanks. But it will not preserve the keys if I will merge the two arrays.

    – Jearson
    Nov 14 '18 at 3:40













  • use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

    – Long Kim
    Nov 14 '18 at 3:43











  • The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

    – Camilo
    Nov 14 '18 at 3:47











  • @camilo editted

    – Long Kim
    Nov 14 '18 at 3:48











  • @LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

    – Jearson
    Nov 14 '18 at 6:53
















4












4








4







You can do something like this



$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);


array_filter will take the 0 out and array_merge will merge 2 arrays together.



You can use



$array3 = array_values(array_filter(array_merge($array1,$array2)));


to reset the keys.



PHP refer for array_filter and array_merge






share|improve this answer















You can do something like this



$array1 = [176, 0];
$array2 = [0, 160];
$array3 = array_filter(array_merge($array1,$array2));
print_r($array3);


array_filter will take the 0 out and array_merge will merge 2 arrays together.



You can use



$array3 = array_values(array_filter(array_merge($array1,$array2)));


to reset the keys.



PHP refer for array_filter and array_merge







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 3:46

























answered Nov 14 '18 at 3:37









Long KimLong Kim

37728




37728













  • Thanks. But it will not preserve the keys if I will merge the two arrays.

    – Jearson
    Nov 14 '18 at 3:40













  • use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

    – Long Kim
    Nov 14 '18 at 3:43











  • The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

    – Camilo
    Nov 14 '18 at 3:47











  • @camilo editted

    – Long Kim
    Nov 14 '18 at 3:48











  • @LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

    – Jearson
    Nov 14 '18 at 6:53





















  • Thanks. But it will not preserve the keys if I will merge the two arrays.

    – Jearson
    Nov 14 '18 at 3:40













  • use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

    – Long Kim
    Nov 14 '18 at 3:43











  • The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

    – Camilo
    Nov 14 '18 at 3:47











  • @camilo editted

    – Long Kim
    Nov 14 '18 at 3:48











  • @LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

    – Jearson
    Nov 14 '18 at 6:53



















Thanks. But it will not preserve the keys if I will merge the two arrays.

– Jearson
Nov 14 '18 at 3:40







Thanks. But it will not preserve the keys if I will merge the two arrays.

– Jearson
Nov 14 '18 at 3:40















use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

– Long Kim
Nov 14 '18 at 3:43





use this $array3 = array_values(array_filter(array_merge($array1,$array2)));

– Long Kim
Nov 14 '18 at 3:43













The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

– Camilo
Nov 14 '18 at 3:47





The actual output of $array3 is not [176, 160] but [0 => 176, 3 => 160].

– Camilo
Nov 14 '18 at 3:47













@camilo editted

– Long Kim
Nov 14 '18 at 3:48





@camilo editted

– Long Kim
Nov 14 '18 at 3:48













@LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

– Jearson
Nov 14 '18 at 6:53







@LongKim What if the arrays are formatted strings? e.g $array1 = [176.00, 0.00]; $array2 = [0.00, 160.00]; How can I achieve this output : [176.00, 160.00]?

– Jearson
Nov 14 '18 at 6:53




















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%2f53292797%2fhow-to-get-total-of-2-arrays%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

Bressuire

Vorschmack

Quarantine