在 php 中创建循环或数组,用于比较来自不同页面的多个会话

Create a loop or array in php for comparing multiple sessions coming from different pages

我有多个页面,每个页面都有多个传递值的单选按钮,在检查所有单选按钮的不同选择后输出一个唯一的结果,目前我正在创建太多难以维护的条件,数组或循环完成它。

<?php
            if(isset($_POST['material'])) {
              $_SESSION['material'] = $_POST['material'];

              // for screw
              if($_SESSION['category'] == "Screw" ) {
                if($_SESSION['headtype'] == "Counter Sink Philips") {
                  if($_SESSION['diameter'] == "6 MM"){
                    if($_SESSION['length'] == "10 MM"){
                      if($_SESSION['pitch'] == "1 MM") {
                        if($_SESSION['material'] == "Brass") {
                          echo "kenenth start with database";
                        }
                      }
                    }
                  }
                }
              }


              // for self tapping
              if($_SESSION['category'] == "Self Tapping" ) {
                if($_SESSION['headtype'] == "Counter Sink Philips") {
                  if($_SESSION['diameter'] == "6 MM"){
                    if($_SESSION['length'] == "10 MM"){
                      if($_SESSION['pitch'] == "1 MM") {
                        if($_SESSION['material'] == "Brass") {
                          echo "Self Tapping";
                        }
                      }
                    }
                  }
                }
              }

              // for stud
              if($_SESSION['category'] == "Stud" ) {
                if($_SESSION['headtype'] == "Counter Sink Philips") {
                  if($_SESSION['diameter'] == "6 MM"){
                    if($_SESSION['length'] == "10 MM"){
                      if($_SESSION['pitch'] == "1 MM") {
                        if($_SESSION['material'] == "Brass") {
                          echo "Stud";
                        }
                      }
                    }
                  }
                }
              }

            }
           ?>

应该这样做

$dataToCheck["category"] = "multipleoptions";
$dataToCheck["headtype"] = "Counter Sink Philips";
// etc...

$found = true;
$foundWhat = "";

foreach ($dataToCheck as $key => $value)
{
     if($key == "category" && ($_SESSION[$key] == "Screw" || $_SESSION[$key] == "Self Tapping" || $_SESSION[$key] == "Stud"))
        {
            $foundWhat = $_SESSION[$key];
            continue;
        }
     else
     {
        found = false;
        break; // Unknown category
     }
     if($_SESSION[$key] != $value )
     {
         $found = false;
         break;
     }
}


if($found == true)
    echo $foundWhat;

您可以编写一个递归函数来为您完成此操作:

function in_array_r($find, $yourArray, $strict = false) {
    foreach ($yourArray as $item) {
        if (($strict ? $item === $find : $item == $find) || (is_array($item) && in_array_r($find, $item, $strict))) {
            return true;
        }
    }

    return false;
}

用法

$a['category'] = array("Screw", "Self Tapping", "Stud");
$a['headtype'] = array("Counter Sink Philips", "Counter Sink Philips"));
echo in_array_r($_SESSION['category'], $a) ? 'found' : 'not found';