Php 替换多维会话数组的变量
我设置了一个多维会话数组。在文件顶部调用sessionstart,并将所有字段设置为示例Php 替换多维会话数组的变量,php,arrays,session,multidimensional-array,Php,Arrays,Session,Multidimensional Array,我设置了一个多维会话数组。在文件顶部调用sessionstart,并将所有字段设置为示例 //set variables $locked="unlocked";$name="BMX";$sport_activity="sport";$quantity="1";$price="600"; //set variables to array $sports_array = array(0 => array( 'i_locked' => $locked, 'i_name' =>
//set variables
$locked="unlocked";$name="BMX";$sport_activity="sport";$quantity="1";$price="600";
//set variables to array
$sports_array = array(0 => array(
'i_locked' => $locked,
'i_name' => $name,
'i_quantity' => $quantity,
'i_price' => $price,
'i_sport_activity' => $sport_activity,
'i_base_price' => $price));
//set multidimensional session array
$_SESSION["activity"][] = $sports_array;
然后在PHP循环中调用该数组
$arrayID = -1;
//foreach loop
foreach($_SESSION['activity'] as $key){
foreach($key as $list){
$arrayID += 1;
?>
//echo all the array items individually in separate divs
<form>
<div>
<?php echo $list['i_locked']?>
</div>
// ..... etc
<input type="hidden" name="ArrayNum" value="<?=$arrayID?>">
<input type='submit' name='Confirm_button'>
</form>
如果您能为最佳实践提供任何帮助或提出正确的建议,我们将不胜感激
*************************重新编辑****************************************
归功于@Suchit Kumar
在他的帮助下,他终于把问题解决了
更改数组元素的第一个问题适用于以下代码。并正确找到需要更改的元素
$_SESSION['activity'][$_POST['ArrayNum']][0]['i_locked'] = 'locked';
向数组中添加新元素的第二个问题与以下代码一起使用
$time = 'pm';
$date = 'feb';
$_SESSION['activity'][$_POST['ArrayNum']][0]['i_time'] = $time;
$_SESSION['activity'][$_POST['ArrayNum']][0]['i_time'] = $date;
我认为如果不使用array('key'=>'value')格式,就不能在array\u push中使用键值对。
在这种情况下,您必须动态执行以下操作:
以及创建$\u会话[“活动”][]=$sports\u数组的方式代码>。您的数组将出现在索引['activity'][0][0]
上
这是一个指出问题的示例,但是您需要通过创建动态索引来动态地遵循这一点
<?php
$time = 'pm';
$date = 'feb';
$_SESSION['activity'][0][0]['i_time']=$time;// when already some elements are there with the key
$_SESSION['activity'][0][0]['i_date']=$date;
echo "<pre>";
print_r($_SESSION);
echo "<pre>";
要更新数组中的任何值,请执行以下操作:
检查是否:
注意:这只是为了展示您在原始代码中的表现。嘿,Suchit,谢谢您的输入。当数组中只有1个元素时效果很好,但是添加另一个数组元素只会影响数组的第一部分。而不是对应的数组部分。很抱歉,我不明白。请详细说明。因此,第一个生效的数组元素正确地添加了日期和时间,根据您的代码,第二个数组元素只影响第一个元素数组中的附加日期和时间1项,以及在日期中添加的附加元素,我们将日期1和时间1作为示例,然后这就是输出数组[0]=(i_lock=hi,i_sport='more',i_date='date1',i_time='time1');但是,当数组中有两个元素时,我们将第二个集合设置为date2和time2数组[0]=(i_locked=hi,i_sport='more',i_date='date2',i_time='time2');数组[1]=(i_locked=hi,i_sport='more');我将发布输出,然后根据您的要求指出您想要的内容。作为对输出的补充,是的,我已经得到了它,并且它可以工作,但是它不适用于添加到堆栈中的任何其他数组元素。i、 当我们转到数组[1]时。任何附加的加法只影响第一个元素数组[0]
<?php
$time = 'pm';
$date = 'feb';
$_SESSION['activity'][0][0]['i_time']=$time;// when already some elements are there with the key
$_SESSION['activity'][0][0]['i_date']=$date;
echo "<pre>";
print_r($_SESSION);
echo "<pre>";
$time1 = 'am';
$date1 = 'mar';
array_push($_SESSION['activity'][$_POST['ArrayNum']],array('i_new'=>$time1,'i_new1'=>$date1));//createing new key and pussing the array.
if(isset($_SESSION['activity'][0][1]['i_time'])){// you can use foreach to access eack key value pair before if condition
$_SESSION['activity'][0][1]['i_time']=$newtime;
}
?>