Add values of two different php arrays











up vote
0
down vote

favorite












My question is about how to add the values from two different arrays, that each have 20 records.



$array1



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)


[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)


$array2



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)


I would like to get a third array like that (adding the values of $array1 and $array2):



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)


How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.



EDIT SOLUTION



$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}









share|improve this question
























  • Looks like your trying to add the count of rows in two SQL queries.
    – Nigel Ren
    Nov 10 at 18:54












  • 70 + 30 = 100 not 1000
    – Madhur Bhaiya
    Nov 10 at 18:54






  • 4




    Better to do this in MySQL than PHP
    – symlink
    Nov 10 at 18:57






  • 1




    This is XY problem. Somewhere else something is incorrect.
    – u_mulder
    Nov 10 at 19:03















up vote
0
down vote

favorite












My question is about how to add the values from two different arrays, that each have 20 records.



$array1



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)


[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)


$array2



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)


I would like to get a third array like that (adding the values of $array1 and $array2):



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)


How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.



EDIT SOLUTION



$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}









share|improve this question
























  • Looks like your trying to add the count of rows in two SQL queries.
    – Nigel Ren
    Nov 10 at 18:54












  • 70 + 30 = 100 not 1000
    – Madhur Bhaiya
    Nov 10 at 18:54






  • 4




    Better to do this in MySQL than PHP
    – symlink
    Nov 10 at 18:57






  • 1




    This is XY problem. Somewhere else something is incorrect.
    – u_mulder
    Nov 10 at 19:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My question is about how to add the values from two different arrays, that each have 20 records.



$array1



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)


[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)


$array2



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)


I would like to get a third array like that (adding the values of $array1 and $array2):



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)


How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.



EDIT SOLUTION



$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}









share|improve this question















My question is about how to add the values from two different arrays, that each have 20 records.



$array1



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 220
[type] => 0
)


[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 70
[type] => 0
)


$array2



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 280
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 30
[type] => 0
)


I would like to get a third array like that (adding the values of $array1 and $array2):



Array
(
[0] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 500
[type] => 0
)

[1] => mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] =>
[num_rows] => 100
[type] => 0
)


How do I do this? All operations I've tried only added the values one after another (new array with 40 rows), not adding them.



EDIT SOLUTION



$array3 = array();
for ($i = 0; $i < count($array2); $i++) {
array_push($array3, $array1[$i]->num_rows + $array2[$i]->num_rows);
}






php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:45

























asked Nov 10 at 18:53









Lama222

82




82












  • Looks like your trying to add the count of rows in two SQL queries.
    – Nigel Ren
    Nov 10 at 18:54












  • 70 + 30 = 100 not 1000
    – Madhur Bhaiya
    Nov 10 at 18:54






  • 4




    Better to do this in MySQL than PHP
    – symlink
    Nov 10 at 18:57






  • 1




    This is XY problem. Somewhere else something is incorrect.
    – u_mulder
    Nov 10 at 19:03


















  • Looks like your trying to add the count of rows in two SQL queries.
    – Nigel Ren
    Nov 10 at 18:54












  • 70 + 30 = 100 not 1000
    – Madhur Bhaiya
    Nov 10 at 18:54






  • 4




    Better to do this in MySQL than PHP
    – symlink
    Nov 10 at 18:57






  • 1




    This is XY problem. Somewhere else something is incorrect.
    – u_mulder
    Nov 10 at 19:03
















Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54






Looks like your trying to add the count of rows in two SQL queries.
– Nigel Ren
Nov 10 at 18:54














70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54




70 + 30 = 100 not 1000
– Madhur Bhaiya
Nov 10 at 18:54




4




4




Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57




Better to do this in MySQL than PHP
– symlink
Nov 10 at 18:57




1




1




This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03




This is XY problem. Somewhere else something is incorrect.
– u_mulder
Nov 10 at 19:03












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1 to $array2:



for ($i = 0; $i < count($array1); $i++) {
$array2[$i]->num_rows += $array1[$i]->num_rows;
}





share|improve this answer





















  • this way the values stayed the same
    – Lama222
    Nov 10 at 19:38










  • @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
    – The fourth bird
    Nov 10 at 19:39








  • 1




    Thanks, got it working with your solution
    – Lama222
    Nov 10 at 19:44


















up vote
0
down vote













if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :



foreach($array1 as $array) {
$array3 = $array;
}





share|improve this answer




























    up vote
    0
    down vote













    Try



    $resultArray = array_merge($array1,$array2);





    share|improve this answer





















    • this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
      – Lama222
      Nov 10 at 19:37










    • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
      – Stephen C
      Nov 11 at 7:02











    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',
    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%2f53242336%2fadd-values-of-two-different-php-arrays%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1 to $array2:



    for ($i = 0; $i < count($array1); $i++) {
    $array2[$i]->num_rows += $array1[$i]->num_rows;
    }





    share|improve this answer





















    • this way the values stayed the same
      – Lama222
      Nov 10 at 19:38










    • @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
      – The fourth bird
      Nov 10 at 19:39








    • 1




      Thanks, got it working with your solution
      – Lama222
      Nov 10 at 19:44















    up vote
    0
    down vote



    accepted










    One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1 to $array2:



    for ($i = 0; $i < count($array1); $i++) {
    $array2[$i]->num_rows += $array1[$i]->num_rows;
    }





    share|improve this answer





















    • this way the values stayed the same
      – Lama222
      Nov 10 at 19:38










    • @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
      – The fourth bird
      Nov 10 at 19:39








    • 1




      Thanks, got it working with your solution
      – Lama222
      Nov 10 at 19:44













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1 to $array2:



    for ($i = 0; $i < count($array1); $i++) {
    $array2[$i]->num_rows += $array1[$i]->num_rows;
    }





    share|improve this answer












    One possible way to do that could be using a for loop and use the index of the arrays to add the num_rows of $array1 to $array2:



    for ($i = 0; $i < count($array1); $i++) {
    $array2[$i]->num_rows += $array1[$i]->num_rows;
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 19:16









    The fourth bird

    18.2k71323




    18.2k71323












    • this way the values stayed the same
      – Lama222
      Nov 10 at 19:38










    • @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
      – The fourth bird
      Nov 10 at 19:39








    • 1




      Thanks, got it working with your solution
      – Lama222
      Nov 10 at 19:44


















    • this way the values stayed the same
      – Lama222
      Nov 10 at 19:38










    • @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
      – The fourth bird
      Nov 10 at 19:39








    • 1




      Thanks, got it working with your solution
      – Lama222
      Nov 10 at 19:44
















    this way the values stayed the same
    – Lama222
    Nov 10 at 19:38




    this way the values stayed the same
    – Lama222
    Nov 10 at 19:38












    @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
    – The fourth bird
    Nov 10 at 19:39






    @Lama222 Did you run a print_r($array2); For example lik 3v4l.org/J8LJd
    – The fourth bird
    Nov 10 at 19:39






    1




    1




    Thanks, got it working with your solution
    – Lama222
    Nov 10 at 19:44




    Thanks, got it working with your solution
    – Lama222
    Nov 10 at 19:44












    up vote
    0
    down vote













    if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :



    foreach($array1 as $array) {
    $array3 = $array;
    }





    share|improve this answer

























      up vote
      0
      down vote













      if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :



      foreach($array1 as $array) {
      $array3 = $array;
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :



        foreach($array1 as $array) {
        $array3 = $array;
        }





        share|improve this answer












        if you want to add every single value of an array($array1) to another array($array3) you need to take a foreach like this :



        foreach($array1 as $array) {
        $array3 = $array;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 18:59









        k-kaufmann

        1




        1






















            up vote
            0
            down vote













            Try



            $resultArray = array_merge($array1,$array2);





            share|improve this answer





















            • this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
              – Lama222
              Nov 10 at 19:37










            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
              – Stephen C
              Nov 11 at 7:02















            up vote
            0
            down vote













            Try



            $resultArray = array_merge($array1,$array2);





            share|improve this answer





















            • this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
              – Lama222
              Nov 10 at 19:37










            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
              – Stephen C
              Nov 11 at 7:02













            up vote
            0
            down vote










            up vote
            0
            down vote









            Try



            $resultArray = array_merge($array1,$array2);





            share|improve this answer












            Try



            $resultArray = array_merge($array1,$array2);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 19:19









            doron

            1




            1












            • this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
              – Lama222
              Nov 10 at 19:37










            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
              – Stephen C
              Nov 11 at 7:02


















            • this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
              – Lama222
              Nov 10 at 19:37










            • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
              – Stephen C
              Nov 11 at 7:02
















            this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
            – Lama222
            Nov 10 at 19:37




            this way the values of the second array add to the first, giving 40 records instead of 20, and the values are not additioned
            – Lama222
            Nov 10 at 19:37












            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – Stephen C
            Nov 11 at 7:02




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – Stephen C
            Nov 11 at 7:02


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242336%2fadd-values-of-two-different-php-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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values