我可以在 php 中的 SESSION 数组上使用 array_push 吗?

发布于 2024-08-28 14:27:14 字数 422 浏览 6 评论 0原文

我有一个想要在多个页面上使用的数组,因此我将其设为 SESSION 数组。我想添加一系列名称,然后在另一个页面上,我希望能够使用 foreach 循环来回显该数组中的所有名称。

这是会话:

$_SESSION['names']

我想使用 array_push 向该数组添加一系列名称,如下所示:

array_push($_SESSION['names'],$name);

我收到此错误:

array_push() [函数.array-push]: 第一个参数应该是一个数组

我可以使用 array_push 将多个值放入该数组中吗?或者也许有更好、更有效的方法来实现我想要实现的目标?

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.

This is the session:

$_SESSION['names']

I want to add a series of names to that array using array_push like this:

array_push($_SESSION['names'],$name);

I am getting this error:

array_push() [function.array-push]:
First argument should be an array

Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧人 2024-09-04 14:27:14

是的,你可以。但第一个参数应该是一个数组。

所以,你必须这样做,

$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);

我个人从不使用 array_push,因为我认为这个函数没有任何意义。我只是用

$_SESSION['names'][] = $name;

Yes, you can. But First argument should be an array.

So, you must do it this way

$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);

Personally I never use array_push as I see no sense in this function. And I just use

$_SESSION['names'][] = $name;
番薯 2024-09-04 14:27:14

尝试用

if (!isset($_SESSION['names'])) {
    $_SESSION['names'] = array();
}
array_push($_SESSION['names'],$name);

Try with

if (!isset($_SESSION['names'])) {
    $_SESSION['names'] = array();
}
array_push($_SESSION['names'],$name);
歌入人心 2024-09-04 14:27:14
 $_SESSION['total_elements']=array();
 array_push($_SESSION['total_elements'], $_POST["username"]);
 $_SESSION['total_elements']=array();
 array_push($_SESSION['total_elements'], $_POST["username"]);
笙痞 2024-09-04 14:27:14

是的!您可以使用array_push推送到会话数组,并且可以通过多种方式根据您的要求访问它们。

基础知识:
array_push 采用前两个参数 array_push($your_array, 'VALUE_TO_INSERT');
请参阅:php 手册以供参考。

示例:
因此,首先您的会话变量应该是一个数组,如下所示:

$arr = array(
    's_var1' => 'var1_value',
    's_var2' => 'var2_value'); // dummy array
$_SESSION['step1'] = $arr;     // session var "step1" now stores array value

现在您可以在 $_SESSION['step1'] 上使用 foreach 循环

foreach($_SESSION['step1'] as $key=>$value) {
    // code here
}

这段代码的好处是您可以使用键名称访问任何数组值,例如:

echo $_SESSION[step1]['s_var1']  // output: var1_value

注意:您还可以使用索引数组进行循环,例如

$arr = array('var1_value', 'var1_value', ....);

奖励:
假设您被重定向到另一个页面
您还可以在您创建的同一数组中插入会话变量。看;

// dummy variables names and values
$_SESSION['step2'] = array(
    's_var3' => 'page2_var1_value',
    's_var4' => 'page2_var2_value');


$_SESSION['step1step2'] = array_merge($_SESSION['step1'], $_SESSION['step2']);

// print the newly created array
echo "<pre>";  // for formatting printed array
var_dump($_SESSION['step1step2']);
echo "<pre>";

输出:

// values are as per my inputs [use for reference only]
array(4) {
  ["s_var1"]=>
  string(7) "Testing"
  ["s_var2"]=>
  int(4) "2124"
  ["s_var3"]=>
  int(4) "2421"
  ["s_var4"]=>
  string(4) "test"
}

*您可以像上面一样在此处使用 foreach 循环从会话变量数组中获取单个会话变量。

eg: 
echo $_SESSION[step1step2]['s_var1'];
OUTPUT:
Testing

希望这有帮助!

Yes! you can use array_push push to a session array and there are ways you can access them as per your requirement.

Basics:
array_push takes first two parameters array_push($your_array, 'VALUE_TO_INSERT');.
See: php manual for reference.

Example:
So first of all your session variable should be an array like:

$arr = array(
    's_var1' => 'var1_value',
    's_var2' => 'var2_value'); // dummy array
$_SESSION['step1'] = $arr;     // session var "step1" now stores array value

Now you can use a foreach loop on $_SESSION['step1']

foreach($_SESSION['step1'] as $key=>$value) {
    // code here
}

The benefit of this code is you can access any array value using the key name for eg:

echo $_SESSION[step1]['s_var1']  // output: var1_value

NOTE: You can also use indexed array for looping like

$arr = array('var1_value', 'var1_value', ....);

BONUS:
Suppose you are redirected to a different page
You can also insert a session variable in the same array you created. See;

// dummy variables names and values
$_SESSION['step2'] = array(
    's_var3' => 'page2_var1_value',
    's_var4' => 'page2_var2_value');


$_SESSION['step1step2'] = array_merge($_SESSION['step1'], $_SESSION['step2']);

// print the newly created array
echo "<pre>";  // for formatting printed array
var_dump($_SESSION['step1step2']);
echo "<pre>";

OUTPUT:

// values are as per my inputs [use for reference only]
array(4) {
  ["s_var1"]=>
  string(7) "Testing"
  ["s_var2"]=>
  int(4) "2124"
  ["s_var3"]=>
  int(4) "2421"
  ["s_var4"]=>
  string(4) "test"
}

*you can use foreach loop here as above OR get a single session var from the array of session variables.

eg: 
echo $_SESSION[step1step2]['s_var1'];
OUTPUT:
Testing

Hope this helps!

半夏半凉 2024-09-04 14:27:14

试试这个,它会起作用的:

session_start();

if(!isset($_POST["submit"]))
{
    $_SESSION["abc"] = array("C", "C++", "JAVA", "C#", "PHP");
}

if(isset($_POST["submit"]))
{
    $aa = $_POST['text1'];

    array_push($_SESSION["abc"], $aa);

    foreach($_SESSION["abc"] as $key => $val)
    { 
        echo $val;
    }
}

Try this, it's going to work :

session_start();

if(!isset($_POST["submit"]))
{
    $_SESSION["abc"] = array("C", "C++", "JAVA", "C#", "PHP");
}

if(isset($_POST["submit"]))
{
    $aa = $_POST['text1'];

    array_push($_SESSION["abc"], $aa);

    foreach($_SESSION["abc"] as $key => $val)
    { 
        echo $val;
    }
}
烛影斜 2024-09-04 14:27:14
<?php
session_start();

$_SESSION['data']= array();
$details1=array('pappu','10');
$details2=array('tippu','12');

array_push($_SESSION['data'],$details1);
array_push($_SESSION['data'],$details2);

foreach ($_SESSION['data'] as $eacharray) 
{
 while (list(, $value) = each ($eacharray)) 
    {
        echo "Value: $value<br>\n";
    }
}
?>

输出

值:pappu
值:10
值:tippu
值:12

<?php
session_start();

$_SESSION['data']= array();
$details1=array('pappu','10');
$details2=array('tippu','12');

array_push($_SESSION['data'],$details1);
array_push($_SESSION['data'],$details2);

foreach ($_SESSION['data'] as $eacharray) 
{
 while (list(, $value) = each ($eacharray)) 
    {
        echo "Value: $value<br>\n";
    }
}
?>

output

Value: pappu
Value: 10
Value: tippu
Value: 12

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文