我想使用php会话添加多个产品

我想使用php会话添加多个产品,php,arrays,session,Php,Arrays,Session,我想把两者都打印出来 <?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 foreac

我想把两者都打印出来

<?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循环将执行两次。它将把数组打印为
$service
数组('1000')
数组('2000')
$\u会话['cart']['prices'][$key]

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

echo$service.'='.$\会话['cart']['prices'][$key].
; }
试试这个:

$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 />';
}
$array1=数组('1000','2000');
$array2=数组('game1','game2');
foreach($array1作为$index=>$key)
{
$\会话['cart']['prices'][]=$key;
$\会话['cart']['services'][]=$array2[$index];
}
foreach($\会话['cart']['services']作为$key=>$service){

echo$service.'='.$\会话['cart']['prices'][$key].
; }
你得到了什么?编辑你的问题。说清楚。你不能有两把相同的钥匙!Error=>E_NOTICE:type 8--数组到字符串的转换--在第11行E_NOTICE:type 8--数组到字符串的转换--在第11行Array=Arrayecho$service.'='$_会话['cart']['prices'][$key].'
//在这行中,再次阅读我的答案…根据当前数据,foreach循环将执行两次。它将打印数组,因为$service是数组('1000'),数组('2000')与$\u会话['cart']['prices'][$key]相同。请给出任何解决方案。实际上,我想在“添加到购物车”页面中添加多个服务和价格