Php 在数组中存储变量数据

Php 在数组中存储变量数据,php,Php,在数组中存储变量数据,然后从数组到会话时,我遇到了一个问题。只有一次,该值存储在索引为0的数组中,但当下一页在“下一步”按钮上加载时,单击以加载下一个测试,该数组的值将被覆盖 <?php session_start(); $cat_name = $_POST["cat_name"]; $which_test_id = $_POST["which_test_id"]; echo $correct = $_POST["correct"]; $answers[] = array('correct

在数组中存储变量数据,然后从数组到会话时,我遇到了一个问题。只有一次,该值存储在索引为0的数组中,但当下一页在“下一步”按钮上加载时,单击以加载下一个测试,该数组的值将被覆盖

<?php session_start();
$cat_name = $_POST["cat_name"];
$which_test_id = $_POST["which_test_id"];
echo $correct = $_POST["correct"];
$answers[] = array('correct' => $correct);
$_SESSION["results"] = $answers;
print_r($_SESSION["results"]); ?>

设置前只需获取答案:)


设置前只需获取答案:)


如果提交,则显示值

<?php session_start();
if(isset($_POST['submit']))
{
$cat_name = $_POST["cat_name"];
$which_test_id = $_POST["which_test_id"];
echo $correct = $_POST["correct"];
$answers[] = array('correct' => $correct);
$_SESSION["results"] = $answers;
}
print_r($_SESSION["results"]); ?>

如果提交,则显示值

<?php session_start();
if(isset($_POST['submit']))
{
$cat_name = $_POST["cat_name"];
$which_test_id = $_POST["which_test_id"];
echo $correct = $_POST["correct"];
$answers[] = array('correct' => $correct);
$_SESSION["results"] = $answers;
}
print_r($_SESSION["results"]); ?>

1.声明数组时不需要“[]”

2.数据被覆盖,因为您没有首先检查数据是否已设置

<?php 
ini_set('session.cookie_domain',"localhost");
session_start();
if(isset($_POST['submit']))
{
    $cat_name = $_POST["cat_name"];
    $which_test_id = $_POST["which_test_id"];
    echo $correct = $_POST["correct"];
    $answers = array('correct' => $correct);
    if(!isset($_SESSION["results"]))
    {
        $_SESSION["results"] = $answers;
    }
}
print_r($_SESSION["results"]); ?>

1.声明数组时不需要“[]”

2.数据被覆盖,因为您没有首先检查数据是否已设置

<?php 
ini_set('session.cookie_domain',"localhost");
session_start();
if(isset($_POST['submit']))
{
    $cat_name = $_POST["cat_name"];
    $which_test_id = $_POST["which_test_id"];
    echo $correct = $_POST["correct"];
    $answers = array('correct' => $correct);
    if(!isset($_SESSION["results"]))
    {
        $_SESSION["results"] = $answers;
    }
}
print_r($_SESSION["results"]); ?>


使用
[]
notation@SaurabhSinha他使用
[]
notation@SaurabhSinha他有。你有没有得到答案?你有没有得到答案?