如何将新的数组值推送到 PHP 会话数组中?

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

How to push a new array value into a PHP session array?

phparrayswordpresssessionarray-push

提问by Keith Donegan

I have the following rough code:

我有以下粗略的代码:

$_SESSION['ids'] = array('strawberry', 'banana', 'apple');

and want to insert some values on a condition (when a person clicks a certain button etc).

并且想要在条件下插入一些值(当一个人点击某个按钮等时)。

array_push($_SESSION['ids'], 'orange');

but it doesn't seem to work?

但它似乎不起作用?

The original array with 'strawberry', 'banana', 'apple' are there when I view a var_dump'edview of it, but 'orange' is not there?

当我查看它的var_dump视图时,原始数组包含“草莓”、“香蕉”、“苹果” ,但“橙色”不存在?

回答by Dykotomee

I have tested the following code on PHP 5.4:

我已经在 PHP 5.4 上测试了以下代码:

session_start();
$_SESSION['ids'] = array('strawberry', 'banana', 'apple');
array_push($_SESSION['ids'], 'orange');
var_dump($_SESSION['ids']);

The output is as follows:

输出如下:

array(1) { ["ids"]=> array(4) { [0]=> string(10) "strawberry" [1]=> string(6) "banana" [2]=> string(5) "apple" [3]=> string(6) "orange" } }

回答by Rajesh Paul

The code you have used should perfectly work even without starting the session. Still giving you the sequence of the statements.

即使不启动会话,您使用的代码也应该可以完美运行。仍然给你陈述的顺序。

code

代码

<?php
    $_SESSION['ids'] = array('strawberry', 'banana', 'apple');
    array_push($_SESSION['ids'], 'mango');
    var_dump($_SESSION);
?>

O/P

开/关

array(1) { ["ids"]=> array(4) { [0]=> string(10) "strawberry" [1]=> string(6) "banana" [2]=> string(5) "apple" [3]=> string(5) "mango" } }

phpfiddle demo

phpfiddle 演示

回答by Ravi Mane

Simple, take an array element. In the following code I made a session element as array element. Using array_push we can add elements. And don't forget to start session.

简单,取一个数组元素。在下面的代码中,我将会话元素作为数组元素。使用 array_push 我们可以添加元素。并且不要忘记开始会话。

 $_SESSION['total_elements']=array();
 array_push($_SESSION['total_elements'], $_POST["username"]);

回答by putvande

As mentioned in the comments, you can push something to the $_SESSION['ids']array like:

正如评论中提到的,您可以将某些内容推送到$_SESSION['ids']数组,例如:

$_SESSION['ids'][] = 'orange';

http://uk1.php.net/array_push

http://uk1.php.net/array_push

回答by Arda

Try this

尝试这个

$_SESSION['ids'] = array('strawberry', 'banana', 'apple');
$_SESSION['ids'][] = 'orange'; 

Tested and its working

测试和它的工作

回答by Anand Solanki

Try this:

尝试这个:

<?php
        $_SESSION['ids'][] = array('strawberry', 'banana', 'apple');
?>

always use bracket when assigning values to array element.

Thanks!

谢谢!

回答by Peter

$_SESSIONis not a super global like other variables. A quick tour on php session handling is given at here, basically you got to call session_start to initialize your session before the array is properly accessible for subsequent writes / reads.

$_SESSION不像其他变量那样是超级全局变量。此处提供了有关 php 会话处理的快速浏览,基本上您必须调用 session_start 来初始化您的会话,然后才能正确访问数组以进行后续写入/读取。

回答by Hyder B.

You can do this :

你可以这样做 :

$_SESSION['ids'] [] = 'orange';