<bdo id='cFLwO'></bdo><ul id='cFLwO'></ul>

        <i id='cFLwO'><tr id='cFLwO'><dt id='cFLwO'><q id='cFLwO'><span id='cFLwO'><b id='cFLwO'><form id='cFLwO'><ins id='cFLwO'></ins><ul id='cFLwO'></ul><sub id='cFLwO'></sub></form><legend id='cFLwO'></legend><bdo id='cFLwO'><pre id='cFLwO'><center id='cFLwO'></center></pre></bdo></b><th id='cFLwO'></th></span></q></dt></tr></i><div id='cFLwO'><tfoot id='cFLwO'></tfoot><dl id='cFLwO'><fieldset id='cFLwO'></fieldset></dl></div>
        <legend id='cFLwO'><style id='cFLwO'><dir id='cFLwO'><q id='cFLwO'></q></dir></style></legend>
        <tfoot id='cFLwO'></tfoot>

        <small id='cFLwO'></small><noframes id='cFLwO'>

        PHP对某个索引相同的多维数组值求和

        PHP Sum multidimensional array values where certain index is the same(PHP对某个索引相同的多维数组值求和)
          • <bdo id='c9AkO'></bdo><ul id='c9AkO'></ul>
              <tbody id='c9AkO'></tbody>

            <small id='c9AkO'></small><noframes id='c9AkO'>

            <i id='c9AkO'><tr id='c9AkO'><dt id='c9AkO'><q id='c9AkO'><span id='c9AkO'><b id='c9AkO'><form id='c9AkO'><ins id='c9AkO'></ins><ul id='c9AkO'></ul><sub id='c9AkO'></sub></form><legend id='c9AkO'></legend><bdo id='c9AkO'><pre id='c9AkO'><center id='c9AkO'></center></pre></bdo></b><th id='c9AkO'></th></span></q></dt></tr></i><div id='c9AkO'><tfoot id='c9AkO'></tfoot><dl id='c9AkO'><fieldset id='c9AkO'></fieldset></dl></div>

              <tfoot id='c9AkO'></tfoot>

              <legend id='c9AkO'><style id='c9AkO'><dir id='c9AkO'><q id='c9AkO'></q></dir></style></legend>

                1. 本文介绍了PHP对某个索引相同的多维数组值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这里只是一个简单的问题.我有以下数组:

                  just a simple problem here. I have the following array:

                  Array(21) {
                  [0] => Array(7) {
                      ["punti"] => Integer  418
                      ["vittorie"] => Integer  9
                      ["podi"] => Integer  18
                      ["gv"] => Integer  14
                      ["id_pilota"] => Integer  1
                      ["team"] => String(15) "Red Bull Racing"
                      ["naz"] => String(2) "it"
                  }
                  [1] => Array(7) {
                      ["punti"] => Integer  353
                      ["vittorie"] => Integer  6
                      ["podi"] => Integer  16
                      ["gv"] => Integer  3
                      ["id_pilota"] => Integer  19
                      ["team"] => String(16) "Scuderia Ferrari"
                      ["naz"] => String(2) "it"
                  }
                  [2] => Array(7) {
                      ["punti"] => Integer  335
                      ["vittorie"] => Integer  4
                      ["podi"] => Integer  15
                      ["gv"] => Integer  1
                      ["id_pilota"] => Integer  5
                      ["team"] => String(12) "Mercedes-AMG"
                      ["naz"] => String(2) "it"
                  }
                  [3] => Array(7) {
                      ["punti"] => Integer  181
                      ["vittorie"] => Integer  1
                      ["podi"] => Integer  5
                      ["gv"] => Integer  1
                      ["id_pilota"] => Integer  2
                      ["team"] => String(12) "Mercedes-AMG"
                      ["naz"] => String(2) "it"
                  }
                  [4] => Array(7) {
                      ["punti"] => Integer  147
                      ["vittorie"] => Integer  0
                      ["podi"] => Integer  3
                      ["gv"] => Integer  0
                      ["id_pilota"] => Integer  14
                      ["team"] => String(15) "Racing Point F1"
                      ["naz"] => String(2) "mx"
                  }
                  [5] => Array(7) {
                      ["punti"] => Integer  127
                      ["vittorie"] => Integer  0
                      ["podi"] => Integer  0
                      ["gv"] => Integer  0
                      ["id_pilota"] => Integer  13
                      ["team"] => String(7) "Haas F1"
                      ["naz"] => String(2) "dk"
                  }
                  

                  减少到 21 个元素.

                  which goes down to 21 elements.

                  我的目标是根据每个飞行员的团队总结所有分数(punti"索引).所以要获得这样的东西:

                  My goal is to sum up all the points ("punti" indexes), based on each pilot's team. So to obtain something like this:

                  Array(10) {
                  [0] => Array(2) {
                      ["team"] => String(...) "Mercedes-AMG")
                      ["points"] => Integer  516
                  

                  我知道这很容易,但是解决此类问题的最快/最方便的方法是什么?

                  I know this is quite easy, but what is the quickest / most convenient way to solve such problem?

                  推荐答案

                  你需要迭代你的结果,当你遇到一个新团队时在输出中添加一个新条目,或者当你找到同一个团队时更新积分值再次.这最容易通过最初按团队名称索引输出,然后使用 array_values 以数字方式重新索引数组:

                  You need to iterate over your results, adding a new entry to the output when you encounter a new team, or updating the points value when you find the same team again. This is most easily done by initially indexing the output by the team name, and then using array_values to re-index the array numerically:

                  $teams = array();
                  foreach ($results as $result) {
                      $team = $result['team'];
                      if (!isset($teams[$team])) {
                          $teams[$team] = array('team' => $team, 'points' => $result['punti']);
                      }
                      else {
                          $teams[$team]['points'] += $result['punti'];
                      }
                  }
                  $teams = array_values($teams);
                  print_r($teams);
                  

                  输出(用于您的样本数据):

                  Output (for your sample data):

                  Array
                  (
                      [0] => Array
                          (
                              [team] => Red Bull Racing
                              [points] => 418
                          )
                      [1] => Array
                          (
                              [team] => Scuderia Ferrari
                              [points] => 353
                          )
                      [2] => Array
                          (
                              [team] => Mercedes-AMG
                              [points] => 516
                          )
                      [3] => Array
                          (
                              [team] => Racing Point F1
                              [points] => 147
                          )
                      [4] => Array
                          (
                              [team] => Haas F1
                              [points] => 127
                          )
                  )
                  

                  3v4l.org 上的演示

                  这篇关于PHP对某个索引相同的多维数组值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  PHP Upload File Validation(PHP 上传文件验证)
                  PHP Error - Uploading a file(PHP 错误 - 上传文件)
                  How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                  php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                  How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                  change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)

                  <small id='kmf2L'></small><noframes id='kmf2L'>

                  1. <tfoot id='kmf2L'></tfoot>
                      <tbody id='kmf2L'></tbody>
                        • <i id='kmf2L'><tr id='kmf2L'><dt id='kmf2L'><q id='kmf2L'><span id='kmf2L'><b id='kmf2L'><form id='kmf2L'><ins id='kmf2L'></ins><ul id='kmf2L'></ul><sub id='kmf2L'></sub></form><legend id='kmf2L'></legend><bdo id='kmf2L'><pre id='kmf2L'><center id='kmf2L'></center></pre></bdo></b><th id='kmf2L'></th></span></q></dt></tr></i><div id='kmf2L'><tfoot id='kmf2L'></tfoot><dl id='kmf2L'><fieldset id='kmf2L'></fieldset></dl></div>

                          <legend id='kmf2L'><style id='kmf2L'><dir id='kmf2L'><q id='kmf2L'></q></dir></style></legend>
                            <bdo id='kmf2L'></bdo><ul id='kmf2L'></ul>