php - 在 php 中创建一个循环或数组来比较来自不同页面的多个 session

标签 php mysql session-variables

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

<?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";
                        }
                      }
                    }
                  }
                }
              }

            }
           ?>

最佳答案

你可以写一个递归函数来为你做这件事:

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';

关于php - 在 php 中创建一个循环或数组来比较来自不同页面的多个 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771838/

相关文章:

php - 在 PHP 中合并正则表达式

php - HTTP 请求失败 所需长度

mysql - JPA Native sql 结果列表转换

php - 在mysql中连接并计算单行中的多行

php - 我想将所有GET数据保存在SESSION中,我该怎么做?

java - 如果我们不允许在 session bean 中使用静态变量,如何定义记录器?

PHP: error: Notice: unserialize(): Error at offset 438 of 750 bytes in 中的 750 个字节的偏移量 438

php - 如何将日期从一种格式转换为另一种格式?

mysql - 如果其他值重复,如何更新 MySQL 表中的值?

javascript - C#/Javascript session 存储