复选框数组与另一个数组的比较(PHP)

复选框数组与另一个数组的比较(PHP),php,arrays,forms,checkbox,comparison,Php,Arrays,Forms,Checkbox,Comparison,我有一个带有几个复选框的页面。您可以进行任意选择组合 <form action="result.php" method="post"> <p> <label> <input type="checkbox" name="CheckboxGroup1[]" value="choice" id="choice"> option 1 </label>

我有一个带有几个复选框的页面。您可以进行任意选择组合

<form action="result.php" method="post">
    <p>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="choice" id="choice">
            option 1
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="range-all" id="range-all">
            option 2
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="range-two" id="range-two">
            option 3
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="range-two" id="range-two">
            option 4
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="range-other-two" id="range-other-two">
            option 5
        </label>
        <br>

    </p>
    <p>
        <input type="submit">
    </p>
</form>


选择1

选择2
选择3
选择4
备选案文5

提交选择后,表单将带您进入下一页,在该页中,它将复选框选择的数组与预定义的选项数组进行比较

<?php

    $options = array("choice", "range-all", "range-two", "range-other-two");
    $checkboxes = $_POST['CheckboxGroup1'];
    $intersection = array_intersect($options, $checkboxes);
    $results = print_r($intersection, true);

    //these are various scenarios based on what the user could choose
    $scen1var = array("choice", "range-all");
    $scen1 = print_r($scen1var, true);

    $scen2var = array("choice", "range-two");
    $scen2 = print_r($scen2var, true);

    $scen3var = array("choice", "range-other-two");
    $scen3 = print_r($scen3var, true);

    $scen4var = array("range-all", "range-other-two");
    $scen4 = print_r($scen4var, true);

    if ($results === $scen1) { 
        echo "choice and range all";
    }
    elseif ($results === $scen2) {
        echo "range  consumables and range  both";
    }
    //The elseif's carry on in this manner
    else {
        echo "something else";
    }

?>

您的数组索引已关闭

将此添加到
if/else
块上方:
print\r($results)


您将看到,选择
choice
rangetwo
将输出
Array([0]=>choice[2]=>rangetwo)
而不是
Array([0]=>choice[1]=>rangetwo)
,这就是
$scene2
的含义。

问题是
Array\u intersect
没有保留数组的键

<?php

$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);

var_dump(array_intersect($array1, $array2));
var_dump(array_intersect($array2, $array1));

?>
你将不得不更换

$scen1var = array("choice", "range-all");
$scen2var = array("choice", "range-two");
$scen3var = array("choice", "range-other-two");
$scen4var = array("range-all", "range-other-two");
为了

我强烈建议你改变处理这个问题的方法,因为这可能不像看上去那么可靠

我的建议是:

$checkboxes = $_POST['CheckboxGroup1'];
if(count($checkboxes) == 2 && 
   in_array('choice', $checkboxes) && 
   in_array('range-all', $checkboxes)
){
    // choice and range only
}
// etc.
当然,有很多方法可以做到这一点。另一种方法

$choice = false;
$range_all = false;
$range_two = false;
$range_other_two = false;
foreach($_POST['CheckboxGroup1'] as $checkbox)
{
    if($checkbox == 'choice')
    {
        $choice = true;
    }
    elseif($checkbox == 'range_all')
    { 
        $range_all = true;
    }
    //etc
}

if($choice && $range_all && !$range_two && !$range_other_two)
{
    // choice and range_all only.
}
// etc.

为什么要使用
print\r()
进行比较?您也可以比较两个数组
$array\u 1===$array\u 2
,不需要吗?嗨,尼科,谢谢您的快速回复。我会改变并让你知道。。。你认为你能为我提出其他研究方法吗?非常感谢!非常感谢你。这对我来说似乎有道理。我要试试看!再次感谢!谢谢你!有道理!
$checkboxes = $_POST['CheckboxGroup1'];
if(count($checkboxes) == 2 && 
   in_array('choice', $checkboxes) && 
   in_array('range-all', $checkboxes)
){
    // choice and range only
}
// etc.
$choice = false;
$range_all = false;
$range_two = false;
$range_other_two = false;
foreach($_POST['CheckboxGroup1'] as $checkbox)
{
    if($checkbox == 'choice')
    {
        $choice = true;
    }
    elseif($checkbox == 'range_all')
    { 
        $range_all = true;
    }
    //etc
}

if($choice && $range_all && !$range_two && !$range_other_two)
{
    // choice and range_all only.
}
// etc.