0

当所有数据通过

$_GET['组'],

$_GET['章节']

$_GET['文章']

与已设置的$laws[$group][$chapter][$article]多维数组不匹配?

我问是因为我打算在$laws 多维数组中一次回显一篇文章,如果不存在这样的数组构造,则会返回错误。

非常感谢!

    <?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];
?>
4

2 回答 2

0

If you want to return FALSE when ALL the data received does not match:

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

But I think you want to return FALSE when ANY of the data received does not match, you should then use:

return isset($laws[$grp][$chap][$art]);
于 2013-02-15T19:07:49.570 回答
0

If you just want to check if the given article exists in the given group and chapter use isset() http://php.net/manual/en/function.isset.php:

$article_exists = isset($laws[$group][$chapter][$article];
于 2013-02-15T19:09:05.647 回答