在codeigniter购物车会话中获取指定的数组


Get specified array inside codeigniter cart session

是否有办法在codeigniter的购物车会话中获得指定的数组?

例如

我有这些值

Array
(
    [c81e728d9d4c2f636f067f89cc14862c] => Array
        (
            [rowid] => c81e728d9d4c2f636f067f89cc14862c
            [id] => 2
            [qty] => 1
            [price] => 1000
            [name] => Pin 
            [subtotal] => 1000
        )
    [c4ca4238a0b923820dcc509a6f75849b] => Array
        (
            [rowid] => c4ca4238a0b923820dcc509a6f75849b
            [id] => 1
            [qty] => 3
            [price] => 100
            [name] => Amber
            [subtotal] => 300
        )
)

,我只想获得数组"c4ca4238a0b923820dcc509a6f75849b"的值

i tried

$this->session->userdata("c81e728d9d4c2f636f067f89cc14862c");

$this->cart->contents("c81e728d9d4c2f636f067f89cc14862c");

但是没有成功

您可以使用$this->cart->contents()获取购物车中的物品以获得特定的密钥,只需像这样调用。

$getCartItem = 'c4ca4238a0b923820dcc509a6f75849b';
$cartItems = $this->cart->contents();
if( isset($cartItems[$getCartItem]) ) 
{
    $item = $cartItems[$getCartItem];
    // do stuff
} else {
    //not found
}

您可以像这样编写自己的函数,

 public function getCartInfo($rowId)
    {
        $cart = $this->cart->contents();
        if( isset($cart[$rowId])) //your $rowId="c81e728d9d4c2f636f067f89cc14862c"
        {
            $data = $cart[$rowId];
            echo "<pre>";
            print_r($data); //view specific cart information
        } 
        else 
        {
            echo "not found";
        }
        exit();
    }