php 如何在php中将数组存储到会话变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18499034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:39:55  来源:igfitidea点击:

How to store an array into a session variable in php

phpmysqlarrayssession

提问by Aayush

//Returns 10 question from questions table

//从问题表中返回10个问题

$result = mysqli_query($con,"SELECT question FROM questions ORDER BY rand() LIMIT 10' ");
while($row = mysqli_fetch_row($result))
{
$que[]=$row[0];
}  

Now I need to store this whole set of $que[]in a session variable. (i.e 10 questions)

现在我需要将这整套存储$que[]在会话变量中。(即10题)

Something like this

像这样的东西

$_SESSION['question'] = $que[]; 

$my_array[] = $_SESSION['question'];

so that $my_array[0]returns first question, $my_array[1]returns second questions and like that.

所以$my_array[0]返回第一个问题,$my_array[1]返回第二个问题等等。

(Thanx for the help in advance)

(感谢提前帮助)

回答by Prasanth Bendra

Assign

分配

$_SESSION['question'] = $que; 

print_r($_SESSION['question'][0]);will give you first question.

print_r($_SESSION['question'][0]);会给你第一个问题。

回答by Jason

You are almost correct, you only need the []when adding to the array.

你几乎是正确的,你只需要[]添加到数组时。

$_SESSION['question'] = $que;

Make sure that you have a session going first, placing this at the top of your script will start a session if one doesn't already exist:

确保你先有一个会话,如果一个会话不存在,将它放在脚本的顶部将启动一个会话:

if( !isset( $_SESSION ) ) {
    session_start();
}

To pull it back up:

把它拉回来:

$array = $_SESSION['question'];  //Assigns session var to $array
print_r($array);                 //Prints array - Cannot use echo with arrays


Final Addition


最后添加

To iterate over the array, you can typically use for, or foreach. For statements really only work well when your array keys are incremental (0, 1, 2, 3, etc) without any gaps.

要遍历数组,通常可以使用 for 或 foreach。For 语句仅在您的数组键是增量(0、1、2、3 等)且没有任何间隙时才真正有效。

for( $x = 0, $max = count($array); $x < $max; ++$x ) {
    echo $array[$x];
}

foreach( $array as &$value ) {
    echo $value;
}

Both have been written in mind for performance. Very important to know that when using a reference (&$value, notice the &) that if you edit the reference, the original value changes. When you do not use by reference, it creates a copy of the value. So for example:

两者都是为了性能而编写的。非常重要的是要知道,在使用引用 ( &$value,注意 &) 时,如果编辑引用,原始值会发生变化。当您不按引用使用时,它会创建该值的副本。例如:

//Sample Array
$array = array( '0' => 5, '1' => 10 );


//By Reference
foreach( $array as &$value ) {
    $value += 2;               //Add 2 to each value
    echo $value;               //Echos 7 and 12, respectively
}
print_r( $array );  //Now equals array( '0' => 7, '1' => 12 )


//Normal Method
foreach( $array as $value ) {
    $value += 2;               //Add 2 to each value
    echo $value;               //Echos 7 and 12, respectively            
}
print_r( $array );  //Still equals array( '0' => 5, '1' => 10 )

References are faster, but not if you are planing on modifying the values while keeping the original array intact.

引用速度更快,但如果您打算在保持原始数组不变的情况下修改值,则不会。

回答by Dabby

use

session_start();
 $_SESSION['question'] = $que;
&que = array(an array of your 10m question s);

when your want to call it on another page to get a line up of your questions, use

当您想在另一个页面上调用它来排列您的问题时,请使用

while (list($key, $value) = each($_SESSION)) {
#Echo the questions using $key
    echo "Here is a list of your questions";
    echo "<br/>";
    while (list($key2, $value2) = each($_SESSION)) {
#$value2 show's name for the indicated ID
#$key2 refers to the ID
         echo "<br/>";
        echo "Question: ".$value2." ";
        echo "<br/>";
    }
    echo "<br/>"; 
}

OR you can also use

或者你也可以使用

print_r;