php 数组作为会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2306159/
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 05:58:35  来源:igfitidea点击:

Array as session variable

phparrayssession

提问by anurag-jain

Is it possible to make an array a session variable in PHP?

是否可以在 PHP 中使数组成为会话变量?

The situation is that I have a table (page 1) with some cells having a link to a particular page. The next page will have a list of names (page 2, which I want to keep in a session array) with their respective checkboxes. On submitting this form, it will lead to a transaction page (page 3, where values of posted checkboxes are kept in a database for corresponding names). Now, if I return to the first page and click another cell, will the session array contain the new list of names or the old ones?

情况是我有一个表格(第 1 页),其中一些单元格具有指向特定页面的链接。下一页将有一个名称列表(第 2 页,我想将其保留在会话数组中)及其各自的复选框。在提交此表单时,它将导致一个交易页面(第 3 页,其中已发布复选框的值保存在数据库中以获取相应名称)。现在,如果我返回第一页并单击另一个单元格,会话数组将包含新的名称列表还是旧的名称列表?

回答by Sarfraz

Yes, you can put arrays in sessions, example:

是的,您可以将数组放入会话中,例如:

$_SESSION['name_here'] = $your_array;

Now you can use the $_SESSION['name_here']on any page you want but make sure that you put the session_start()line before using any session functions, so you code should look something like this:

现在您可以$_SESSION['name_here']在任何您想要的页面上使用 ,但请确保session_start()在使用任何会话函数之前放置该行,因此您的代码应如下所示:

 session_start();
 $_SESSION['name_here'] = $your_array;

Possible Example:

可能的例子:

 session_start();
 $_SESSION['name_here'] = $_POST;

Now you can get field values on any page like this:

现在您可以在任何页面上获取字段值,如下所示:

 echo $_SESSION['name_here']['field_name'];

As for the second part of your question, the session variables remain there unless you assign different array data:

至于问题的第二部分,除非您分配不同的数组数据,否则会话变量将保留在那里:

 $_SESSION['name_here'] = $your_array;

Session life time is set into php.inifile.

会话生存时间设置在php.ini文件中。

More Info Here

更多信息在这里

回答by Kaleb Brasee

Yes, PHP supports arrays as session variables. See this pagefor an example.

是的,PHP 支持数组作为会话变量。有关示例,请参阅此页面

As for your second question: once you set the session variable, it will remain the same until you either change it or unsetit. So if the 3rd page doesn't change the session variable, it will stay the same until the 2nd page changes it again.

至于你的第二个问题:一旦你设置了会话变量,它就会保持不变,直到你改变它或unset它。因此,如果第 3 页不更改会话变量,它将保持不变,直到第 2 页再次更改它。

回答by Sanjoy

session_start();          //php part
$_SESSION['student']=array();
$student_name=$_POST['student_name']; //student_name form field name
$student_city=$_POST['city_id'];   //city_id form field name
array_push($_SESSION['student'],$student_name,$student_city);   
//print_r($_SESSION['student']);


<table class="table">     //html part
    <tr>
      <th>Name</th>
      <th>City</th>
    </tr>

    <tr>
     <?php for($i = 0 ; $i < count($_SESSION['student']) ; $i++) {
     echo '<td>'.$_SESSION['student'][$i].'</td>';
     }  ?>
    </tr>
</table>

回答by muhammad sirajo

First change the array to a string by using implode() function. E.g $number=array(1,2,3,4,5,...); $stringofnumber=implode("|",$number);then pass the string to a session. e.g $_SESSION['string']=$stringofnumber;so when you go to the page where you want to use the array, just explode your string. e.g $number=explode("|", $_SESSION['string']);finally number is your array but remember to start array on the of each page.

首先使用 implode() 函数将数组更改为字符串。例如,$number=array(1,2,3,4,5,...); $stringofnumber=implode("|",$number);然后将字符串传递给会话。例如$_SESSION['string']=$stringofnumber;,当您转到要使用数组的页面时,只需分解您的字符串。例如, $number=explode("|", $_SESSION['string']);最终数字是您的数组,但请记住在每一页的开始数组。