通过array_keys()获取Array Entry


Obtain Array Entry Using array_keys()

我试图获得第一个条目,如果在$_POST$_SESSION中没有设置任何内容,但当我使用die(print_r($nmcu));输出以下内容时,我总是只得到1,如果我将数字添加到"条目1"answers"出口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];
?>

清理会话(或使用var_dump($_SESSION)检查)。我很确定你在那里有一个无效的键,所以print_r()什么也不打印,并返回, which is then outputed by die() '。

此外,我建议使用var_dump()而不是print_r()进行此类测试。