我正在使用CI框架PHP,
我想根据四个用户输入决策做出最终决策结果,
$this->input->post('decision_1', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_2', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_3', TRUE) == 0 //or 1 or 2 or 3
$this->input->post('decision_4', TRUE) == 0 //or 1 or 2 or 3
现在,如果任何三个输入相同,那么它将成为最终决定,或者在四个或三个输入中,最大决策将成为最终决定,
示例,如果决策1,2,3为0,则final应为0 但如果第1,2号决定为0而第4号决定为1,则最终也应为1。
我尝试使用switch语句,但如果所有最小3个输入都相同,它确实有效,但如果三个不同,那么我就不起作用了,
$result_case = TRUE;
switch ($result_case) {
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1 :
$result = 1;
break;
case $this->input->post('decision_1', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
case $this->input->post('decision_2', TRUE) == 1 && $this->input->post('decision_3', TRUE) == 1 && $this->input->post('decision_4', TRUE) == 1:
$result = 1;
break;
//
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_1', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
case $this->input->post('decision_2', TRUE) == 0 && $this->input->post('decision_3', TRUE) == 0 && $this->input->post('decision_4', TRUE) == 0:
$result = 0;
break;
//
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_1', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
case $this->input->post('decision_2', TRUE) == 2 && $this->input->post('decision_3', TRUE) == 2 && $this->input->post('decision_4', TRUE) == 2:
$result = 2;
break;
default:
$result = 'Undecided';
}
有没有简单的方法来检查输入决策并做出最终决定?
谢谢,
答案 0 :(得分:1)
//save each post in as a key of an array.
$results[$this->input->post('decision_1', TRUE)][] = 1;
...
...
...
//count each type of the post
$results = array_map(function($v){return count($v);}, $results;);
$max = -1;
foreach($results as $k => $v)
{
if($v >= 3)
$result = $k;
$max = $results[$max] > $v ? $max : $k;
}
//if a post type has more or equal to 3, chose it. otherwise chose the biggest key as the result.
$result = isset($result) ? $result : $max;