将php中的数组从后置变为会话变量 [英] Altering arrays in php from post to a session variable

查看:108
本文介绍了将php中的数组从后置变为会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我发布了此处并对如何处理阵列做出了很好的回应。
这正是我所需要的

$ foreach($ _POST ['name'] as $ key => $名称){
echo名称:$ name年龄:{$ _POST ['age'] [$ key]};
}

问题是,我不需要打印,但需要保留。使会话变量像 $ _ SESSION [name] =some name;



我想知道如何我可以将POST数组从上面转储到$ SESSION [Array]中;
,以便能够在任何页面上随意转储所有数据。我需要访问数组中的$ name和$ age,并且我希望它们可以关联。
在java中我会这样做

$ p $ String [] [] something = new String [10] [2]; // 10是大小,2允许索引0处的名称和索引1处的年龄。

要牢记。 POST数组的大小未设置。他们可能在0输入到100的任何地方。



在保存数组的同时,请告诉我如何访问它。我使用java,所以php数组对我来说是新的。



编辑



试用后johnps post



我的页面看起来像这样

  foreach($ _POST ['name'] as $ key => ; $ name){
echo名称:$ name年龄:{$ _POST ['age'] [$ key]}< br />;
$ _SESSION ['post_data'] [$ key] = $ name;
$ _SESSION ['post_data'] [$ key] = $ _POST ['age'];
echo $ key;
}

并且页面上的输出是

 名称:名称药水年龄:成人
1

但是,Ive试图通过以下方式获得名称位置作为输出并且没有任何工作正在进行

pre $ code> echo $ _SESSION ['post_data'] ['name']; //没有输出
echo $ _SESSION ['post_data'] [$ key]; //输出Array b

我希望输出为
名称位置,成人。
就像foreach循环现在正在做的一样,但是来自二维数组。感谢你们。

解决方案

您可以在PHP中使用多维数组。你的例子不起作用的原因是因为它保持覆盖相同的索引

  //假设你的POST数据由age = >名称对,你像这样存储它
$ _SESSION ['post_data'] = array();
$ count = 0;
foreach($ _POST ['name'] as $ age => $ name){
$ _SESSION ['post_data'] [$ count] ['age'] = $ age;
$ _SESSION ['post_data'] [$ count] ['name'] = $ name;
$ count ++;
}

要访问它,只需遍历它或使用键

  //迭代
foreach($ _SESSION ['post_data'] as $ person){
echoName: {$ person ['name']}年龄:{$ person ['age']}< br />;
}

//使用键
echo $ _SESSION ['post_data'] [0] ['age']; //打印第一个人的年龄
echo $ _SESSION ['post_data'] [0] ['name']; //打印第一个人的名字

在视觉上你的数据看起来像这样(例子)

  array(
0 => array(
age => 12,
name => jane
),
1 =>数组(
age => 18,
name => jack
),
2 =>数组(
age => 25,
name => jones
),


a couple of days ago, I posted here and got a great response on how to handle the arrays. This was exactly what I needed

foreach ($_POST['name'] as $key=>$name) {
  echo "Name: $name  Age: {$_POST['age'][$key]}";
}

the problem is, I need it not to print but to persist. Im making session variables like $_SESSION["name"]= "some name";

I wanna know how I can dump the POST array from above into a $SESSION[Array]; ultimately, to be able to re dump all of the data at will on any page. I need access to both $name in the array and the $age and I would like them to be associated. In java I would do

String[][] something = new String[10][2]; //10 is size and 2 allows for name at index 0 and age at index 1. 

Something things to keep in mind. The size of the POST array is not set. They may be anywhere from 0 inputs to 100.

Along with saving the array, could you please tell me how to access it. Im use to java, so php arrays are new to me.

EDIT

After Trying johnps post

my page looks like this

foreach ($_POST['name'] as $key=>$name) {
  echo "Name: $name  Age: {$_POST['age'][$key]} <br/>";
     $_SESSION['post_data'][$key] = $name;
     $_SESSION['post_data'][$key] = $_POST['age'];
    echo $key; 
}

and the output on the page is

Name: The name potion Age: adult
1

However, Ive tried the following to get "The Name Position" as output and nothing is working

echo $_SESSION['post_data']['name'];//doesnt have an output
echo $_SESSION['post_data'][$key];//out puts "Array" and nothing else

I would like the output to be The name Position, Adult. Just like the foreach loop is doing right now, but from a 2d array. Thanks guys.

解决方案

You can use a multi dimensional array just as easily in PHP. The reason your example doesn't work is because it keeps overriding the same index

//Assuming your POST data consists of age => name pairs, you store it like this
$_SESSION['post_data'] = array();
$count = 0;
foreach ($_POST['name'] as $age => $name) {
   $_SESSION['post_data'][$count]['age']  = $age;
   $_SESSION['post_data'][$count]['name'] = $name;
   $count++;
}

And to access it, simply iterate over it or use the key

//iterating
foreach ($_SESSION['post_data'] as $person) {
     echo "Name: {$person['name']}  Age: {$person['age']} <br/>";
}

//using the key
echo $_SESSION['post_data'][0]['age']; //print the first person's age
echo $_SESSION['post_data'][0]['name']; //print the first person's name

Visually your data looks like this (example)

array(
  0 => array(
    age  => 12,
    name => jane
  ),
  1 => array(
    age  => 18,
    name => jack
  ),
  2 => array(
    age  => 25,
    name => jones
  ),
)

这篇关于将php中的数组从后置变为会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆