【问题标题】:Store array of objects in PHP session在 PHP 会话中存储对象数组
【发布时间】:2014-01-25 10:12:20
【问题描述】:

如何在 PHP 会话中存储/删除/获取对象数组?

我试过这个来添加:

array_push($_SESSION['cart'], serialize($item));    

这个用于移除:

function removeitem ($item)
{
    $arrayCart = $_SESSION['cart'] ;
    for ($i=0; $i<$arrayCart.length; $++ )
    {
        if ($item.id == $arrayCart[i].id)
            $arrayCart.splice (i,0); 
    }
}

但是不行!!

【问题讨论】:

  • php是从什么时候开始使用点符号的??????你混淆了js和php。我建议买一本书,并使用 ide
  • 我看到的最明显的事情是使用序列化 - 此函数用于写入文件或数据库 - 如果您的数据是本机 php,则将其保留为 php

标签: php session session-variables


【解决方案1】:
<?php
session_start();

$_SESSION['cart']=array();

$_SESSION['cart']['username'] = 'Admin';
$_SESSION['cart']['Password'] = '123';
.
.
.
?>


对于删除

    <?php
    session_start();

    $_SESSION['cart']=array();

   unset ( $_SESSION['cart']['username'] );
    .
    .

or use Custom Function to Remove...
    .
    ?>

这对我有用...
如果此代码报告错误请评论以检查此...
请检查PHP版本

【讨论】:

    【解决方案2】:

    怎么没人看到这个:

    for ($i=0; $i<$arrayCart.length; $++ ){ //  <-----  lol @ $++
    

    设为$i++

    你还需要另外 2 个 } 来关闭你的 if()for() 语句,所以它是这样的:

    function removeitem ($item){
        $arrayCart = $_SESSION['cart'] ;
        for ($i=0; $i<$arrayCart.length; $++ ){
            if ($item.id == $arrayCart[i].id){
                $arrayCart.splice (i,0); 
            }
        }
    }
    

    【讨论】:

    • 是的,你是对的。我猜代码在您更改后会起作用,但是效率不是很高
    • 同意,但下面的@Tom 给出了更有用的答案
    • 此外,如果客户多次添加一种产品,它也会多次存储在购物车中。如果 item-id 用作 Array-Index,则不会发生这种情况。 (并且搜索项目也变得更加高效)
    【解决方案3】:

    你试过用这个吗?

    if(!isset($_SESSION['cart'])){
      $_SESSION['cart'] = [];
    }
    $_SESSION['cart'][] = $item;
    

    您不需要序列化 ​​item-variable - Session 可以存储纯对象,只要 max.会话大小不超过。

    如果您想从购物车中删除商品,您应该使用 index-Names - 这可以让您更快地找到您的商品。

    我可能会使用这样的东西:

    if(!isset($_SESSION['cart'])){
      $_SESSION['cart'] = [];
    }
    
    function addItem($itemName){
        if(!isset($_SESSION['cart'][$itemName])) {
            $_SESSION['cart'][$itemName] = 1;
        }else
            $_SESSION['cart'][$itemName] ++;
        }
    }
    function removeItem($itemName){
        if(isset($_SESSION['cart'][$itemName])) {
            if($_SESSION['cart'][$itemName] > 0) {
                $_SESSION['cart'][$itemName] --;
            }
        }
    }
    
    function clearCart(){
       unset($_SESSION['cart']);
    }
    

    在这种情况下,购物车会存储每件商品及其数量。除了$itemName,您还可以使用数据库中的项目ID。

    要从购物车中获取商品,您将需要此函数:

    function getItemCount($itemName){
        if(isset($_SESSION['cart'][$itemName])) {
            return $_SESSION['cart'][$itemName];
        }
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      尽量不要推送到数组,因为它可能尚未设置,但要存储到会话,请通过以下方式创建新会话变量:

      $_SESSION['cart']=serialize($item);
      

      删除它:

      unset($_SESSION['cart']);
      

      编辑:

      感谢 cmets,我意识到上面的代码不会在会话中存储数组。因此,另一个简短的解决方案是将一些 JSON 存储到会话变量中。试试这个做一个简短的测试:

      $tmp = array(1,2,3);
      $_SESSION['cart']=json_encode($tmp);
      print_r(json_decode($_SESSION['cart']));
      

      如果该数组是关联的,则应该用于解码:

      json_decode($_SESSION['cart'], true);
      

      【讨论】:

      • 但是这样我的会话中只有一个对象..?
      • 这将只允许购物车中的一项。
      猜你喜欢
      • 2017-12-06
      • 2012-01-26
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      相关资源
      最近更新 更多