2017-03-18 52 views
1

我使用CI框架PHP,請根據四個輸入決定最終的決定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,那麼最後應該是0 ,但如果決定1 ,2是0並且判定4是1,則f伊納勒也應該是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'; 
    } 

有什麼簡單的方法來檢查輸入的決定,並做出最後一個?

感謝,

回答

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; 
+0

我來到這裏感到困惑,我有四個輸入與三個不同的可能性,所以我需要寫12線$結果[...],如果你給($ v> = 3),這是否意味着它會檢查最少3個輸入來做出決定?如果有三個決定,我需要最終結果,否則默認決定應該是3 – rjcode

+0

,每次只有四個輸入。每個用戶都有一個願望。所以每一次最大的是4. –

+0

好極了,似乎沒問題,只是問一件事,如果所有3個決定都不一樣,就像0,1,2 ....那麼我需要通過一個默認決定3(未定),我如何發送默認決定?在這種情況下,沒有結果輸出爲$ result。 – rjcode

相關問題