使用多维数组匹配表单数据

时间:2013-02-15 18:56:53

标签: php

当通过

收到所有数据时,我应该使用什么验证码来返回FALSE

$ _ GET [ '基团'],

$ _ GET [ '章']

$ _ GET [ '文章']

不符合 $ laws [$ group] [$ chapter] [$ article] 多维数组已设置?

我问,因为我打算在 $ laws multidimensional array 中一次回复一篇文章,如果这样的数组结构不存在,则会返回错误。

非常感谢!

    <?php

session_start();
$laws = array(
    "group1" => array(
        "1" => array(
            "1" => "This is article (1) in chapter (1) of (group1)",
            "2" => "This is article (2) in chapter (1) of (group1)",
            "3" => "This is article (3) in chapter (1) of (group1)",
        ),
        "2" => array(
            "1" => "This is article (1) in chapter (2) of (group1)",
            "2" => "This is article (2) in chapter (2) of (group1)",
            "3" => "This is article (3) in chapter (2) of (group1)",
        ),
    ),
    "group2" => array(
        "1" => array(
            "1" => "This is article (1) in chapter (1) of (group2)",
            "2" => "This is article (2) in chapter (1) of (group2)",
            "3" => "This is article (3) in chapter (1) of (group2)",
        ),
        "2" => array(
            "1" => "This is article (1) in chapter (2) of (group2)",
            "2" => "This is article (2) in chapter (2) of (group2)",
            "3" => "This is article (3) in chapter (2) of (group2)",
        ),
    )
);


$_SESSION['group'] = $_GET['group'];
$_SESSION['chapter'] = $_GET['chapter'];
$_SESSION['article'] = $_GET['article'];

$group = $_SESSION['group'];
$chapter = $_SESSION['chapter'];
$article = $_SESSION['article'];



// Echo Article from $laws multidimensional Array

echo $laws[$group][$chapter][$article];
?>

2 个答案:

答案 0 :(得分:0)

如果要在收到的所有数据都不匹配时返回FALSE:

$grp= $_GET['group'];
$chap = $_GET['chapter'];
$art = $_GET['article'];
return isset($laws[$grp]) || isset($laws[$grp][$chap]) || isset($laws[$grp][$chap][$art]);

但我认为你想在收到的任何数据不匹配时返回FALSE,你应该使用:

return isset($laws[$grp][$chap][$art]);

答案 1 :(得分:0)

如果您只想检查给定组中是否存在给定文章,请使用isset()http://php.net/manual/en/function.isset.php

$article_exists = isset($laws[$group][$chapter][$article];