PHP SESSION数组仅存储在索引0中

时间:2013-10-08 13:56:47

标签: php session

嘿伙计这是我正在使用的代码

$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][] = $_POST['t0'];
$_SESSION[$ch][] = $_POST['t1'];
$_SESSION[$ch][] = $_POST['t2'];
$_SESSION[$ch][] = $_POST['t3'];
$_SESSION[$ch][] = $_POST['t4'];
$_SESSION[$ch][] = $_POST['t5'];
$_SESSION[$ch][] = $_POST['t6'];

但是,当我在屏幕$_SESSION['s1'][0]上打印会话数据时,所有其他索引的所有数据都已显示,$_SESSION['s1'][1]$_SESSION['s1'][6]没有显示任何内容。

以下是var_dump($_SESSION);

的结果
's1' => array (size=7) 
   0 => string 'All Purpose Horn Anvil A-1t1=Shipping Weight: 900gmst2=Price: € 5.00t3=Quantity: t4= t5=56t6=All Purpose Horn Anvil A-1 Shipping Weight: 900gms Price: € 5.00 Quantity: 56' (length=192) 
    1 => null 
    2 => null 
    3 => null 
    4 => null 
    5 => null 
    6 => null

由于某种原因,POST索引,即t0到t6已在s1中显示。

4 个答案:

答案 0 :(得分:1)

您可以在其他地方覆盖该数据,或者只是在打印时出错。

$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
$_SESSION[$ch][] = mt_rand(1,200);
print_r( $_SESSION );

输出:

  

数组([s1] =>数组([0] => 158 [1] => 145 [2] => 110 [3] => 139   [4] => 153 [5] => 183 [6] => 51))

答案 1 :(得分:1)

将数据传递给POST是一个错误。 在Jquery,我没有使用“&”变量之间,导致问题。

var dataString = 't0=' + txt[0]+ '&t1='+ txt[1] + '&t2='+ txt[2] + '&t3='+ txt[3] + '&t4='+ txt[4] + '&t5='+ txt[5] + '&t6='+ temp;

答案 2 :(得分:0)

尝试在相等之前添加一个点:

    $ch="s1";
    $_SESSION[$ch] = array();

    $_SESSION[$ch][] .= $_POST['t0'];
    $_SESSION[$ch][] .= $_POST['t1'];
    $_SESSION[$ch][] .= $_POST['t2'];
    $_SESSION[$ch][] .= $_POST['t3'];
    $_SESSION[$ch][] .= $_POST['t4'];
    $_SESSION[$ch][] .= $_POST['t5'];
    $_SESSION[$ch][] .= $_POST['t6'];

您可以在此链接(3v4l.org/8ZDi9)上看到此代码:

<?php

    $ch="s1";
    $_SESSION[$ch] = array();

    $_SESSION[$ch][] .= 11;
    $_SESSION[$ch][] .= 22;

    var_dump($_SESSION);

答案 3 :(得分:0)

$ch="s1";
$_SESSION[$ch] = array();
$_SESSION[$ch][0] = $_POST['t0'];
$_SESSION[$ch][1] = $_POST['t1'];
$_SESSION[$ch][2] = $_POST['t2'];
$_SESSION[$ch][3] = $_POST['t3'];
$_SESSION[$ch][4] = $_POST['t4'];
$_SESSION[$ch][6] = $_POST['t5'];
$_SESSION[$ch][7] = $_POST['t6'];

或更好

$ch="s1";
$_SESSION[$ch] = array();


for ($i=0; $i<=6; $i++)
{
   $_SESSION[$ch][$i] = $_POST['t'.$i];
}