我想用php session添加多个产品

时间:2015-09-18 09:27:19

标签: php arrays session

我想打印两个

<button type="button">

1 个答案:

答案 0 :(得分:0)

更好地利用这个:

$_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 />';
}