【问题标题】:Make final decision based on four input decisions php根据四个输入决策做出最终决定 php
【发布时间】:2017-03-18 07:06:04
【问题描述】:

我正在使用 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';
  }

是否有任何简单的方法来检查输入决定并做出最终决定?

谢谢,

【问题讨论】:

    标签: php codeigniter switch-statement


    【解决方案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;
    

    【讨论】:

    • 我在这里搞糊涂了,我有四个输入,三个不同的可能性,所以我需要为 $results[....] 写 12 行,你给 if ($v >= 3 ),这是否意味着它将检查至少 3 个输入以做出决定?如果有三个决定,我需要最终结果,否则默认决定应该是 3
    • 每次只有四个输入。每个用户一个愿望。所以每次最大的是4。
    • 太棒了,似乎还可以,只是问一件事,如果所有 3 个决定都不同,例如 0,1,2....那么我需要通过默认决定 3(未决定),如何我可以发送默认决定吗?在这种情况下,没有任何东西作为 $result 输出。
    • 从 $results 中,你可以得到这样的 $decision3 = isset($results[3]) ? $results[3] : null;
    • 你能解释一下 $max 在 foreach 循环中的用途吗?因为它没有用于 $results 计算?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2021-07-13
    • 2018-08-03
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多