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