如何创建多维数组并使用会话

时间:2018-03-07 12:53:02

标签: php arrays session

我正在尝试为我的网站创建购物车,并希望能够将产品添加到购物篮中。

所以我在cart.php文件中创建了一个ajax帖子,如果它不存在我会在其中启动会话,然后添加到会话中,或者只在会话中添加会话。

当我没有像这样指定键时,这是有效的:

if(isset($_POST['product'])){
  if (isset($_SESSION['cart'])) {
    $_SESSION['cart'][] = $_POST['product'];
    $_SESSION['cart'][] = $_POST['price'];
    $_SESSION['cart'][] = $_POST['picture'];
    $_SESSION['cart'][] = $_POST['quantity'];
 } else {
    //Session is not set, setting session now
    $_SESSION['cart'] = array();
    $_SESSION['cart'][] = $_POST['product'];
    $_SESSION['cart'][] = $_POST['price'];
    $_SESSION['cart'][] = $_POST['picture'];
    $_SESSION['cart'][] = $_POST['quantity'];
 }
}

这给了我一个这样的数组:

Array
(
    [0] => Douche 1
    [1] => 1200
    [2] => cms/images/douche.jpg
    [3] => 
    [4] => Douche 1
    [5] => 1200
    [6] => cms/images/douche.jpg
    [7] => 
    [8] => Douche 1
    [9] => 1200
    [10] => cms/images/douche.jpg
    [11] => 
    [12] => Douche 1
    [13] => 1200
    [14] => cms/images/douche.jpg
    [15] => 
)

但是后来我想循环结果,所以我需要键才能使用值而不仅仅是数字。所以我尝试了这个:

if(isset($_POST['product'])){
  if (isset($_SESSION['cart'])) {
    $_SESSION['cart']['product'] = $_POST['product'];
    $_SESSION['cart']['price'] = $_POST['price'];
    $_SESSION['cart']['picture'] = $_POST['picture'];
    $_SESSION['cart']['quantity'] = $_POST['quantity'];
 } else {
    //Session is not set, setting session now
    $_SESSION['cart'] = array();
    $_SESSION['cart']['product'] = $_POST['product'];
    $_SESSION['cart']['price'] = $_POST['price'];
    $_SESSION['cart']['picture'] = $_POST['picture'];
    $_SESSION['cart']['quantity'] = $_POST['quantity'];
 }
}

这给了我以下结果:

Array
(
    [product] => Douche 1
    [price] => 1200
    [picture] => cms/images/douche.jpg
    [quantity] => 
)

它每次都会替换它自己,所以我需要的是一个多维数组。我的问题:我该如何创建它?

这是我想要的结果:

Array
(
    [0] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] => 
        )

    [1] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] =>  => 18
        )
)

3 个答案:

答案 0 :(得分:3)

只需将数据数组附加到会话中:

$product = array(
    'product' => _POST['product'],
    'price' => $_POST['price'];
    'picture' => $_POST['picture'];
    'quantity' => $_POST['quantity'];
);
$_SESSION['cart'][] = $product;

答案 1 :(得分:2)

使用第二次尝试,试试这个:

if(isset($_POST['product'])){
  $thisProduct = array(
    'product' => _POST['product'],
    'price' => $_POST['price'],
    'picture' => $_POST['picture'],
    'quantity' => $_POST['quantity'],
  );
  if (isset($_SESSION['cart'])) {
    $_SESSION['cart'][] = $thisProuct;
  } else {
    //Session is not set, setting session now
    $_SESSION['cart'] = array();
    $_SESSION['cart'][] = $thisProuct;
  }
}

答案 2 :(得分:1)

试试这个,

if (isset($_POST['product'])) {
    $_SESSION['cart'][] = [
        'product'  => $_POST['product'],
        'price'    => $_POST['price'],
        'picture'  => $_POST['picture'],
        'quantity' => $_POST['quantity'],
    ];
}