在codeigniter cart会话中获取指定的数组

时间:2014-08-25 12:46:36

标签: php arrays codeigniter session cart

有没有办法在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”

我试过

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

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

但它没有用

2 个答案:

答案 0 :(得分:3)

您可以使用$this->cart->contents()获取购物车商品以获取特定密钥,只需按此方式调用即可。

$getCartItem = 'c4ca4238a0b923820dcc509a6f75849b';
$cartItems = $this->cart->contents();

if( isset($cartItems[$getCartItem]) ) 
{
    $item = $cartItems[$getCartItem];
    // do stuff

} else {
    //not found
}

答案 1 :(得分:0)

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

 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();
    }