我想使用php session添加多个产品

Sanjog米塔尔

我想同时打印

<?php

//in file A
$_SESSION['cart']['prices'] = array('1000');
$_SESSION['cart']['services'] = array('game');

//In File B
$_SESSION['cart']['prices'] = array('2000');
$_SESSION['cart']['services'] = array('game2');

//in file C
foreach ($_SESSION['cart']['services'] as $key => $service) {
    echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}
?>
快乐编码

最好使用这个:

$_SESSION['cart']['prices'][] = array('1000');
$_SESSION['cart']['services'][] = array('game');

//In File B
$_SESSION['cart']['prices'][] = array('2000');
$_SESSION['cart']['services'][] = array('game2');

根据当前数据,foreach循环将执行两次。这将打印数组作为$servicearray array('1000')array('2000')相同$_SESSION['cart']['prices'][$key]

foreach ($_SESSION['cart']['services'] as $key => $service) {
    echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}

尝试这个 :

$array1 = array('1000','2000');
$array2 = array('game1','game2');

foreach($array1 as $index=>$key)
{
    $_SESSION['cart']['prices'][] = $key;
    $_SESSION['cart']['services'][] = $array2[$index];
}



foreach ($_SESSION['cart']['services'] as $key => $service) {
    echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章