我有一个数组,其中一些学科名称是相同的,我想得到相同学科名称的crypt_count之和。
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
)
)发布于 2018-03-23 11:05:47
尝试使用foreach循环迭代数组,并为总计构建一个最终数组:
$totals = [];
foreach($array as $arr) {
if (!isset($totals[$arr['discipline_name']]) {
$totals[$arr['discipline_name']] = 0;
}
$totals[$arr['discipline_name']] += $arr['crypt_count'];
}可能需要针对特定的用例进行调整/清理,但基本逻辑应该可以工作。
发布于 2018-03-23 11:31:29
我的方法可能有点不同,理解起来有点复杂,但另一方面,它更灵活。
本质上,这将每个discipline_name的所有数据(或您将来抛出的任何索引)合并到一个数组中。这样可以方便地进行数据操作。
// 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 )
https://stackoverflow.com/questions/49448172
复制相似问题