使用array_keys()获取数组输入

时间:2013-05-22 10:50:23

标签: php arrays

我想在$_POST$_SESSION中设置任何内容时尝试获取第一个条目,但是当我使用die(print_r($nmcu));输出以下内容时,如果我添加数字,我总是只获得1它似乎有用,但我不知道为什么,我不想要名字中的数字...

到“入口1”和“出口2”的结尾
<?php
session_start();
$entry = array(
"entry" => array("user" => "username",
        "pass" => "password",
        "host" => "localhost",
        "port" => 1111,
        "protocol" => "http"),

"exit" => array("user" => "username",
        "pass" =>   "password",
        "host" =>   "localhost",
        "port" =>   1111,
        "protocol" => "http"));


if (isset($_POST['currentEntry'])) {
    $_SESSION['currentEntry'] = $_POST['currentEntry'];
}
if (isset($_SESSION['currentEntry'])) {
    $currentEntry = $_SESSION['currentEntry'];
} else {
    $keys = array_keys($entry);
    $currentEntry = $keys[0];
    $_SESSION['currentEntry'] = $currentEntry;
}
$nmcu = $entry[$currentEntry];
?>

1 个答案:

答案 0 :(得分:1)

清理会话(或使用var_dump($_SESSION)进行检查。我确定您的密钥无效,因此print_r()不打印任何内容并返回, which is then outputed by die() `。

另外,我建议使用var_dump()代替print_r()进行此类测试。