How to sum 90+ Arrays with using function?How can I prevent SQL injection in PHP?In PHP, how do you change the key of an array element?Deleting an element from an array in PHPSecure hash and salt for PHP passwordsGet the first element of an arrayReference — What does this symbol mean in PHP?How do I check if a string contains a specific word?PHP multidimensional array search by valuePHP array delete by value (not key)Why shouldn't I use mysql_* functions in PHP?

How to emphasise the insignificance of someone/thing – besides using "klein"

What is Soda Fountain Etiquette?

How could a self contained organic body propel itself in space

Why was this commercial plane highly delayed mid-flight?

Videos of surgery

Is Nikon d500 a good fit for nature and ambient-lighting portraits and occasional other uses?

How to pass 2>/dev/null as a variable?

Shift lens vs move body?

How do I insert two edge loops equally spaced from the edges?

Dotted background on a flowchart

Could the UK amend the European Withdrawal Act and revoke the Article 50 invocation?

Why did the population of Bhutan drop by 70% between 2007 and 2008?

Can a character use multiple reactions in response to the same trigger?

Recommended Breathing Exercises to Play Woodwinds

Can I take a boxed bicycle on a German train?

How to say "I only speak one which is English." in French?

Is there an in-universe explanation given to the senior Imperial Navy Officers as to why Darth Vader serves Emperor Palpatine?

Half filled water bottle

Looking for a plural noun related to ‘fulcrum’ or ‘pivot’ that denotes multiple things as crucial to success

Is this password scheme legit?

Why does this London Underground poster from 1924 have a Star of David atop a Christmas tree?

Why did James Cameron decide to give Alita big eyes?

Time difference between banns and marriage

If I said I had $100 when asked, but I actually had $200, would I be lying by omission?



How to sum 90+ Arrays with using function?


How can I prevent SQL injection in PHP?In PHP, how do you change the key of an array element?Deleting an element from an array in PHPSecure hash and salt for PHP passwordsGet the first element of an arrayReference — What does this symbol mean in PHP?How do I check if a string contains a specific word?PHP multidimensional array search by valuePHP array delete by value (not key)Why shouldn't I use mysql_* functions in PHP?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards



Array 
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)


I want to get only one array result like:



Array 
(
[1] => 3
[2] => 2
[3] => 1
)









share|improve this question


























  • I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

    – kuh-chan
    Mar 27 at 20:41












  • @kuh-chan Main post edited.

    – xomod
    Mar 27 at 20:44











  • Again, what does this even mean arrays without name ???

    – AbraCadaver
    Mar 27 at 20:45











  • @AbraCadaver That means i have only that 90+ arrays without any sum system.

    – xomod
    Mar 27 at 20:46











  • Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

    – kuh-chan
    Mar 27 at 20:47

















0















I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards



Array 
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)


I want to get only one array result like:



Array 
(
[1] => 3
[2] => 2
[3] => 1
)









share|improve this question


























  • I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

    – kuh-chan
    Mar 27 at 20:41












  • @kuh-chan Main post edited.

    – xomod
    Mar 27 at 20:44











  • Again, what does this even mean arrays without name ???

    – AbraCadaver
    Mar 27 at 20:45











  • @AbraCadaver That means i have only that 90+ arrays without any sum system.

    – xomod
    Mar 27 at 20:46











  • Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

    – kuh-chan
    Mar 27 at 20:47













0












0








0








I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards



Array 
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)


I want to get only one array result like:



Array 
(
[1] => 3
[2] => 2
[3] => 1
)









share|improve this question
















I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards



Array 
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)


I want to get only one array result like:



Array 
(
[1] => 3
[2] => 2
[3] => 1
)






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 20:43







xomod

















asked Mar 27 at 20:37









xomodxomod

133 bronze badges




133 bronze badges















  • I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

    – kuh-chan
    Mar 27 at 20:41












  • @kuh-chan Main post edited.

    – xomod
    Mar 27 at 20:44











  • Again, what does this even mean arrays without name ???

    – AbraCadaver
    Mar 27 at 20:45











  • @AbraCadaver That means i have only that 90+ arrays without any sum system.

    – xomod
    Mar 27 at 20:46











  • Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

    – kuh-chan
    Mar 27 at 20:47

















  • I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

    – kuh-chan
    Mar 27 at 20:41












  • @kuh-chan Main post edited.

    – xomod
    Mar 27 at 20:44











  • Again, what does this even mean arrays without name ???

    – AbraCadaver
    Mar 27 at 20:45











  • @AbraCadaver That means i have only that 90+ arrays without any sum system.

    – xomod
    Mar 27 at 20:46











  • Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

    – kuh-chan
    Mar 27 at 20:47
















I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

– kuh-chan
Mar 27 at 20:41






I have a script that dynamically creates arrays without name. and how are these arrays stored? Maybe you should tell us what exactly your goal is

– kuh-chan
Mar 27 at 20:41














@kuh-chan Main post edited.

– xomod
Mar 27 at 20:44





@kuh-chan Main post edited.

– xomod
Mar 27 at 20:44













Again, what does this even mean arrays without name ???

– AbraCadaver
Mar 27 at 20:45





Again, what does this even mean arrays without name ???

– AbraCadaver
Mar 27 at 20:45













@AbraCadaver That means i have only that 90+ arrays without any sum system.

– xomod
Mar 27 at 20:46





@AbraCadaver That means i have only that 90+ arrays without any sum system.

– xomod
Mar 27 at 20:46













Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

– kuh-chan
Mar 27 at 20:47





Again: how are they stored? Do you write the arrays dynamically in a file? If so - how?

– kuh-chan
Mar 27 at 20:47












2 Answers
2






active

oldest

votes


















0















Add all your arrays into one array.



$all_arrays[] =Array 
(
[1] => 1 ,
[2] => 1,
[3] => 1
);

$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;

$all_arrays[] = Array
(
[1] => 1
);

$results = [];
foreach($all_arrays as $arr)
foreach($arr as $user_id=>$value)
if(in_array($user_id,$results))
$results[$user_id] = $results[$user_id] + $value;
else
$results[$user_id] = $value;








share|improve this answer
































    0















    You would need to specify all arrays in the array_merge:



    $result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));


    If you can get the arrays dynamically added to another $main_array like:



    Array
    (
    [0] => Array
    (
    [1] => 1
    [2] => 1
    [3] => 1
    )

    [1] => Array
    (
    [1] => 1
    [2] => 1
    )

    [2] => Array
    (
    [1] => 1
    )

    )


    Then it would be much easier:



    $result = array_count_values(array_keys(array_merge(...$main_array)));





    share|improve this answer



























      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%2f55386036%2fhow-to-sum-90-arrays-with-using-function%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









      0















      Add all your arrays into one array.



      $all_arrays[] =Array 
      (
      [1] => 1 ,
      [2] => 1,
      [3] => 1
      );

      $all_arrays[] = Array
      (
      [1] => 1,
      [2] => 1
      ) ;

      $all_arrays[] = Array
      (
      [1] => 1
      );

      $results = [];
      foreach($all_arrays as $arr)
      foreach($arr as $user_id=>$value)
      if(in_array($user_id,$results))
      $results[$user_id] = $results[$user_id] + $value;
      else
      $results[$user_id] = $value;








      share|improve this answer





























        0















        Add all your arrays into one array.



        $all_arrays[] =Array 
        (
        [1] => 1 ,
        [2] => 1,
        [3] => 1
        );

        $all_arrays[] = Array
        (
        [1] => 1,
        [2] => 1
        ) ;

        $all_arrays[] = Array
        (
        [1] => 1
        );

        $results = [];
        foreach($all_arrays as $arr)
        foreach($arr as $user_id=>$value)
        if(in_array($user_id,$results))
        $results[$user_id] = $results[$user_id] + $value;
        else
        $results[$user_id] = $value;








        share|improve this answer



























          0














          0










          0









          Add all your arrays into one array.



          $all_arrays[] =Array 
          (
          [1] => 1 ,
          [2] => 1,
          [3] => 1
          );

          $all_arrays[] = Array
          (
          [1] => 1,
          [2] => 1
          ) ;

          $all_arrays[] = Array
          (
          [1] => 1
          );

          $results = [];
          foreach($all_arrays as $arr)
          foreach($arr as $user_id=>$value)
          if(in_array($user_id,$results))
          $results[$user_id] = $results[$user_id] + $value;
          else
          $results[$user_id] = $value;








          share|improve this answer













          Add all your arrays into one array.



          $all_arrays[] =Array 
          (
          [1] => 1 ,
          [2] => 1,
          [3] => 1
          );

          $all_arrays[] = Array
          (
          [1] => 1,
          [2] => 1
          ) ;

          $all_arrays[] = Array
          (
          [1] => 1
          );

          $results = [];
          foreach($all_arrays as $arr)
          foreach($arr as $user_id=>$value)
          if(in_array($user_id,$results))
          $results[$user_id] = $results[$user_id] + $value;
          else
          $results[$user_id] = $value;









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 20:50









          altoidsaltoids

          3883 gold badges9 silver badges28 bronze badges




          3883 gold badges9 silver badges28 bronze badges


























              0















              You would need to specify all arrays in the array_merge:



              $result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));


              If you can get the arrays dynamically added to another $main_array like:



              Array
              (
              [0] => Array
              (
              [1] => 1
              [2] => 1
              [3] => 1
              )

              [1] => Array
              (
              [1] => 1
              [2] => 1
              )

              [2] => Array
              (
              [1] => 1
              )

              )


              Then it would be much easier:



              $result = array_count_values(array_keys(array_merge(...$main_array)));





              share|improve this answer





























                0















                You would need to specify all arrays in the array_merge:



                $result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));


                If you can get the arrays dynamically added to another $main_array like:



                Array
                (
                [0] => Array
                (
                [1] => 1
                [2] => 1
                [3] => 1
                )

                [1] => Array
                (
                [1] => 1
                [2] => 1
                )

                [2] => Array
                (
                [1] => 1
                )

                )


                Then it would be much easier:



                $result = array_count_values(array_keys(array_merge(...$main_array)));





                share|improve this answer



























                  0














                  0










                  0









                  You would need to specify all arrays in the array_merge:



                  $result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));


                  If you can get the arrays dynamically added to another $main_array like:



                  Array
                  (
                  [0] => Array
                  (
                  [1] => 1
                  [2] => 1
                  [3] => 1
                  )

                  [1] => Array
                  (
                  [1] => 1
                  [2] => 1
                  )

                  [2] => Array
                  (
                  [1] => 1
                  )

                  )


                  Then it would be much easier:



                  $result = array_count_values(array_keys(array_merge(...$main_array)));





                  share|improve this answer













                  You would need to specify all arrays in the array_merge:



                  $result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));


                  If you can get the arrays dynamically added to another $main_array like:



                  Array
                  (
                  [0] => Array
                  (
                  [1] => 1
                  [2] => 1
                  [3] => 1
                  )

                  [1] => Array
                  (
                  [1] => 1
                  [2] => 1
                  )

                  [2] => Array
                  (
                  [1] => 1
                  )

                  )


                  Then it would be much easier:



                  $result = array_count_values(array_keys(array_merge(...$main_array)));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 20:51









                  AbraCadaverAbraCadaver

                  62.2k7 gold badges43 silver badges70 bronze badges




                  62.2k7 gold badges43 silver badges70 bronze badges






























                      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%2f55386036%2fhow-to-sum-90-arrays-with-using-function%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현