我正在使用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,那么final也应该是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';
}有什么简单的方法来检查输入决策并做出最终的决定吗?
谢谢,
发布于 2017-03-18 07:28:37
//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;https://stackoverflow.com/questions/42871561
复制相似问题