我没有在PHP中获得二维数组的概念。我正在尝试实施一个购物车系统,其中数组会话变量存储productid及其数量。对于每个新条目,如果存在数量,则应增加其数量,或者如果不存在,则应添加新ID。这是我的初始代码。
function cart_increment_ajax($data, $qtt) {
$_SESSION['count']+=$qtt;
set_cart( $data );
echo $_SESSION['count'];
}
function initialise_cart( ) {
$_SESSION['cart'] =array( );
$_SESSION['totalprice'] = 0;
}
function set_cart( $pid ) {
if(!isset($_SESSION['cart'])) {
initialise_cart( );
}
//else if( int $_SESSION['cart'] pid exists increment count ))
else
// ($_SESSION['cart'] add new pid.
}
我没有得到如何通过多维关联数组来实现评论的行?
在会话中多阵列的一个小快的n脏示例
<?php
function add_to_cart($product_id,$count)
{
// no need for global $_SESSION is superglobal
// init session variable cart
if (!isset($_SESSION['cart']))
$_SESSION['cart'] = array();
// check if product exists
if (!isset($_SESSION['cart'][$product_id]))
$_SESSION['cart'][$product_id]=$count;
else
$_SESSION['cart'][$product_id]+=$count;
}
// add some foos and a bar
add_to_cart('foo',2);
add_to_cart('foo',1);
add_to_cart('bar',1);
print_r($_SESSION['cart']);
?>
这将产生
Array
(
[foo] => 3
[bar] => 1
)
hth
将产品ID用作数组中的索引,然后只需使用 。
$_SESSION['cart'][$pid]++;