这个问题似乎有些重复,但我发誓现在已经尝试过数千种解决方案......
我有一个像这样的关联多维数组:
1 =>
array (size=1)
'Pld' =>
array (size=2)
'score_good_answers' => string '1' (length=1)
'score_bad_answers' => string '0' (length=1)
2 =>
array (size=1)
'Aln' =>
array (size=2)
'score_good_answers' => string '0' (length=1)
'score_bad_answers' => string '1' (length=1)
3=>
array (size=1)
'IPS' =>
array (size=2)
'score_good_answers' => string '1' (length=1)
'score_bad_answers' => string '0' (length=1)
4 =>
array (size=1)
'Pld' =>
array (size=2)
'score_good_answers' => string '1' (length=1)
'score_bad_answers' => string '0' (length=1)
5 =>
array (size=1)
'Aln' =>
array (size=2)
'score_good_answers' => string '1' (length=1)
'score_bad_answers' => string '0' (length=1)
6 =>
array (size=1)
'Aln' =>
array (size=2)
'score_good_answers' => string '1' (length=1)
'score_bad_answers' => string '0' (length=1)
FOR Var_Export
:
1=> array ( 'Pld' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 2 => array ( 'Aln' => array ( 'score_good_answers' => '0', 'score_bad_answers' => '1', ), ), 3 => array ( 'IPS' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 4 => array ( 'Pld' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 5 => array ( 'Aln' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 6 => array ( 'Aln' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ),
我需要对所有'score_good_answers'
和'score_bad_answers'
以及所有Aln
和Pld
等进行总结。
更糟糕的情况是,这个键,即:是可变值。
在这个级别,任何有效的解决方案都将受到赞赏。
我在这个SO问题中尝试了接受的答案:How to sum values of the array of the same key?
然而,它似乎抛出了几个错误......
我从几个SO问题中尝试了几个解决方案, 即:How to sum values of the array of the same key?,Array sum of value based on same key,How to sum values via the same key and group by other key等等......
另一个很接近的是:How to sum same array in php
并尝试过:
$array2 = array();
for($f = 0; $f<count($getCategories); $f++){
foreach($getCategories[$i] as $k=>$v) {
if(!isset($array2[$v['Aln']])) {
$array2[$v['Aln']] = $v;
} else {
$array2[$v['Aln']]['score_good_answers'] += $v['score_good_answers'];
}
}
} var_dump($array2);
我仍然会收到多个Errors
,包括invalid arguments
undefined offsets
等等
谦卑地请求任何建议。
非常感谢
答案 0 :(得分:1)
您可以使用此代码:
$answers = array();
foreach ($getCategories as $categories){
foreach($categories as $category => $scores){
foreach ($scores as $type => $score){
if (isset($answers[$category][$type])){
$answers[$category][$type] += (int) $score;
} else {
$answers[$category][$type] = (int) $score;
}
}
}
}
输出将是以下数组:
Array
(
[Pld] => Array
(
[score_good_answers] => 2
[score_bad_answers] => 0
)
[Aln] => Array
(
[score_good_answers] => 2
[score_bad_answers] => 1
)
[IPS] => Array
(
[score_good_answers] => 1
[score_bad_answers] => 0
)
)
答案 1 :(得分:0)
保存密钥的变量名为$ f,但是您尝试在下一行使用未定义的变量$ i,请参阅?这是你的一个警告,不会做你想要的。在第二行使用$ f。
for($f = 0; $f<count($getCategories); $f++){
foreach($getCategories[$i] as $k=>$v) {
您的数据结构有点奇怪吗?第二个最顶层阵列中是否只有一个键?
如果可能的话,这会更漂亮:
array(2) {
[0] => stdClass#1 (3) {
public $name => string(4) "Pld"
public $score_good_answers => int(1)
public $score_bad_answers=> int(0)
}
[1] => stdClass#1 (3) {
public $name => string(4) "Aln"
public $score_good_answers => int(0)
public $score_bad_answers=> int(1)
}
}
我看不出你想要的最终结果,但尝试一下,可能不符合你的想法。
$goodAnswersByCategoryDataKey = array();
$badAnswersByCategoryDataKey = array();
foreach ($categories as $i => $category) {
foreach ($category as $categoryDataKey => $categoryData) {
if (!isset($goodAnswersByCategoryDataKey[$categoryDataKey])) {
$goodAnswersByCategoryDataKey[$categoryDataKey] = 0;
}
$goodAnswersByCategoryDataKey[categoryDataKey] += $categoryData['score_good_answers'];
if (!isset($badAnswersByCategoryDataKey[$categoryDataKey])) {
$badAnswersByCategoryDataKey[$categoryDataKey] = 0;
}
$badAnswersByCategoryDataKey[$categoryDataKey] += $categoryData['score_bad_answers'];
}
}
var_dump(goodAnswersByCategoryDataKey);
var_dump(badAnswersByCategoryDataKey);
答案 2 :(得分:0)
如果它是一个像你的例子中那样的迭代结构,你可以这样做:
$answers = [];
$nbCat = count($getCategories);
for($i = 0; $i < $nbCat; $i++) {
foreach($getCategories[$i] as $key => $scores) {
if(empty($answers[$key])) {
$answers[$key] = $scores;
}
else {
$answers[$key]['score_good_answers'] += $scores['score_good_answers'];
$answers[$key]['score_bad_answers'] += $scores['score_bad_answers'];
}
}
}