Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 每个用户的阵列数据_Php_Arrays - Fatal编程技术网

Php 每个用户的阵列数据

Php 每个用户的阵列数据,php,arrays,Php,Arrays,我试图从数组中保存的值返回一个和 例如,总额为: '答案一=>0=>0'+'答案二=>0=>0' 返回值将是这些值的总和,在第一个问题的例子中,它将是1500 是否有方法调整以下内容以在for循环中返回值 电流输出: "answer_one" => array:3 [ 0 => array:3 [▼ 0 => 1500 1 => 0 2 => 0 ] 1 => array:3 [▼ 0 => 0 1 => 788 2 =&g

我试图从数组中保存的值返回一个和

例如,总额为:

'答案一=>0=>0'+'答案二=>0=>0'

返回值将是这些值的总和,在第一个问题的例子中,它将是1500

是否有方法调整以下内容以在for循环中返回值

电流输出:

"answer_one" => array:3 [
0 => array:3 [▼
  0 => 1500
  1 => 0
  2 => 0
]
1 => array:3 [▼
  0 => 0
  1 => 788
  2 => 0
]
2 => array:3 [▼
  0 => 0
  1 => 0
  2 => 651
]
]
  "answer_two" => array:3 [
    0 => array:3 [▼
      0 => 0
      1 => 1500
      2 => 0
    ]
    1 => array:3 [▶]
    2 => array:3 [▶]
  ]
当前代码

$users = \App\User::where('role_id', '!=', 1)->get();

    for ($k = 1; $k < setting('amount_of_questions'); $k++) {
        $f = str_replace(' ', '', $this->convertNumberToWord($k));
        $tests = 'question_' . $f . '_answer';
        $atts[$tests] = array();

        foreach ($users as $userr) {
            if ($userr->$tests == 'For') {
                $test = array(intval($userr->question_value), 0, 0);
                array_push($atts[$tests], $test);
            } else if ($userr->$tests == 'Against') {
                $test = array(0, intval($userr->question_value), 0);
                array_push($atts[$tests], $test);
            } else if ($userr->$tests == 'Abstain') {
                $test = array(0, 0, intval($userr->question_value));
                array_push($atts[$tests], $test);
            }
        }

    }
$users=\App\User::where('role_id','!=',1)->get();
对于($k=1;$k<设置($u问题的数量);$k++){
$f=str_replace(“”,,$this->convertNumberToWord($k));
$tests='question.'$f.'u answer';
$atts[$tests]=array();
foreach($users作为$userr){
如果($userr->$tests=='For'){
$test=array(intval($userr->question_值),0,0);
阵列推送($atts[$tests],$test);
}else if($userr->$tests==‘反对’){
$test=array(0,intval($userr->question\u值),0);
阵列推送($atts[$tests],$test);
}else if($userr->$tests==‘弃权’){
$test=数组(0,0,intval($userr->question_value));
阵列推送($atts[$tests],$test);
}
}
}
了解


演示:

不清楚您想要什么。您能提供所需输出的样本吗?另外,你能描述一下你自己在做这件事时遇到的问题吗?@El_Vanja细节都在帖子里。计算有点错误。这将返回嵌套数组的总值。我需要做的是得到[0]的所有值并将它们相加,然后[1]等等。有意义吗?是的,确实如此,这可能吗?!这很好@Codesee是否有任何方法可以将它们分组,因此“问题1的分数为”-“问题1的分数为”-可能结构错误,但基本上在我的数据库中,我有答案字段、“问题1的答案”、“问题2的答案”等。每个用户都投票一个值,该值保存在另一个字段中。有三种可能的答案,反对弃权。如果用户对问题1投了赞成票,则该值将分配给该用户。如果他们对问题2投反对票,也将分配给该问题。我们正在计算这个问题的总价值。因此,如果有5个用户在第一个问题上投了赞成票,我们需要将这些值相加,得到总数。如果有两个用户投了“反对票”——这些总数。最终目标是:问题1(For):$q1fortotal;问题1(针对):共计$Q1;问题1(弃权):共计100美元;问题2(a):共计2 200美元;问题2(反对):共计2美元;问题2(弃权):共计200美元;
$sum = array("answer_one" => array("For" => 0, "Against" => 0, "Abstain" => 0), "answer_two" => array("For" => 0, "Against" => 0, "Against" => 0));
foreach ($atts as $questionNum => $questionAnswers) { // $questionNum = answer_one then answer_two
   foreach($questionAnswers as $questionAnswer => $answerValues) { // each users response to a question (it will be an array with 3 values)
      foreach($answerValues as $answerNum => $answerValue) {
          if($answerValue > 0) {
             if($answerNum == 0) {
                $sum[$questionNum]["For"] += $answerValue;
             }else if($answerNum == 1) {
                $sum[$questionNum]["Against"] += $answerValue;
             }else if($answerNum == 2) {
                $sum[$questionNum]["Abstain"] += $answerValue;
             }
          }
      }
   }
}

$questionNum = 1;
foreach($sum as $questionAnswers) {
   foreach($questionAnswers as $questionAnswer => $questionAnswerTotal) {
      echo 'The score for question '.$questionNum.' '.$questionAnswer.' is '.$questionAnswerTotal.'. ';
   }
   $questionNum++;
}