首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想在php中得到多维数组中两个数组键的和。

我想在php中得到多维数组中两个数组键的和。
EN

Stack Overflow用户
提问于 2018-03-23 10:59:50
回答 2查看 183关注 0票数 1

我有一个数组,其中一些学科名称是相同的,我想得到相同学科名称的crypt_count之和。

代码语言:javascript
复制
Array
(
 [0] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 3
    )

[1] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 1
    )

[2] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 3
    )

[3] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 2
    )

[4] => Array
    (
        [discipline_name] => Dementation
        [library_count] => 1
        [crypt_count] => 0
    )

[5] => Array
    (
        [discipline_name] => Obfuscate
        [library_count] => 0
        [crypt_count] => 2
    )

[6] => Array
    (
        [discipline_name] => Fortitude
        [library_count] => 0
        [crypt_count] => 3
    )

[7] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 0
        [crypt_count] => 1
    )

[8] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 0
        [crypt_count] => 1
    )

[9] => Array
    (
        [discipline_name] => Celerity
        [library_count] => 0
        [crypt_count] => 1
    )

[10] => Array
    (
        [discipline_name] => Redemption
        [library_count] => 1
        [crypt_count] => 0
    )

[11] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[12] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 6
        [crypt_count] => 0
    )

[13] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[14] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 4
        [crypt_count] => 0
    )

[15] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 4
        [crypt_count] => 0
    )

[16] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 2
        [crypt_count] => 0
    )

[17] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[18] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 1
        [crypt_count] => 0
    )

[19] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 3
        [crypt_count] => 0
    )

[20] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 2
        [crypt_count] => 0
    )

[21] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[22] => Array
    (
        [discipline_name] => Auspex
        [library_count] => 1
        [crypt_count] => 0
    )

[23] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 4
        [crypt_count] => 0
    )

[24] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[25] => Array
    (
        [discipline_name] => Potence
        [library_count] => 1
        [crypt_count] => 0
    )

[26] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 1
        [crypt_count] => 0
    )

[27] => Array
    (
        [discipline_name] => Necromancy
        [library_count] => 2
        [crypt_count] => 0
    )

[28] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 0
        [crypt_count] => 1
    )

[29] => Array
    (
        [discipline_name] => Dominate
        [library_count] => 0
        [crypt_count] => 1
    )

[30] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 4
    )

[31] => Array
    (
        [discipline_name] => Obtenebration
        [library_count] => 0
        [crypt_count] => 2
    )

[32] => Array
    (
        [discipline_name] => Potence
        [library_count] => 0
        [crypt_count] => 1
    )

[33] => Array
    (
        [discipline_name] => Potence
        [library_count] => 0
        [crypt_count] => 1
    )

)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-23 11:05:47

尝试使用foreach循环迭代数组,并为总计构建一个最终数组:

代码语言:javascript
复制
$totals = [];
foreach($array as $arr) {
    if (!isset($totals[$arr['discipline_name']]) {
        $totals[$arr['discipline_name']] = 0;
    }
    $totals[$arr['discipline_name']] += $arr['crypt_count'];
}

可能需要针对特定的用例进行调整/清理,但基本逻辑应该可以工作。

票数 2
EN

Stack Overflow用户

发布于 2018-03-23 11:31:29

我的方法可能有点不同,理解起来有点复杂,但另一方面,它更灵活。

本质上,这将每个discipline_name的所有数据(或您将来抛出的任何索引)合并到一个数组中。这样可以方便地进行数据操作。

代码语言:javascript
复制
// Your array data
$json = '[{"discipline_name":"Fortitude","library_count":"0","crypt_count":"3"},{"discipline_name":"Fortitude","library_count":"0","crypt_count":"1"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"3"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"2"},{"discipline_name":"Dementation","library_count":"1","crypt_count":"0"},{"discipline_name":"Obfuscate","library_count":"0","crypt_count":"2"},{"discipline_name":"Fortitude","library_count":"0","crypt_count":"3"},{"discipline_name":"Necromancy","library_count":"0","crypt_count":"1"},{"discipline_name":"Necromancy","library_count":"0","crypt_count":"1"},{"discipline_name":"Celerity","library_count":"0","crypt_count":"1"},{"discipline_name":"Redemption","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"6","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Obtenebration","library_count":"4","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"4","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"3","crypt_count":"0"},{"discipline_name":"Obtenebration","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Auspex","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"4","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Potence","library_count":"1","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"1","crypt_count":"0"},{"discipline_name":"Necromancy","library_count":"2","crypt_count":"0"},{"discipline_name":"Dominate","library_count":"0","crypt_count":"1"},{"discipline_name":"Dominate","library_count":"0","crypt_count":"1"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"4"},{"discipline_name":"Obtenebration","library_count":"0","crypt_count":"2"},{"discipline_name":"Potence","library_count":"0","crypt_count":"1"},{"discipline_name":"Potence","library_count":"0","crypt_count":"1"}]';
$data = json_decode($json, true);

// Nifty function to restructure the array
// It is flexible and thus reuseable code, so I recommend defining it someone that is loaded always.
function array_groupby($a, $i, $rem = true) {
  foreach($a as $v){
    $k = $v[$i];
    if($rem){
      unset($v[$i]);
    }
    $t[$k][] = $v;
  }

  return $t;
}

// The actual code.
// Loop over a new array that is grouped by name
foreach(array_groupby($data, 'discipline_name') as $deciple => $values){
  // this will merge the column value of the count to a totals.
  $totals[$deciple] = array_sum(array_column($values,'crypt_count'));
}

print_r($totals);

Array ( [Fortitude] => 7 [Obtenebration] => 11 [Dementation] => 0 [Obfuscate] => 2 [Necromancy] => 2 [Celerity] => 1 [Redemption] => 0 [Dominate] => 2 [Auspex] => 0 [Potence] => 2 )

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49448172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档