简体   繁体   English

如何使用PHP插入新的数组键值并存储在会话中?

[英]How to insert a new array key value and store in a session using PHP?

I need a little help here. 我需要一点帮助。 In my page I have a summary product list and in that page I need to store all my parent id in a session. 在我的页面中,有一个产品摘要列表,在该页面中,我需要在会话中存储所有父ID。 But only the last id I can store. 但是只有我可以存储的最后一个ID。 How can I store all the products ID as the key and the quantity as a values in a session? 如何在会话中存储所有产品ID作为键,并将数量存储为值?

Here's my sample code: 这是我的示例代码:

fprintr($get_product);

        foreach($get_product as $product_detail) {

            if ($product_detail['image']) {
                $image = $this->model_tool_image->resize($product_detail['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
            } else {
                $image = false;
            }

            $search_data[] = array(
                'product_id' => $product_detail['product_id'],
                'code' => $product_detail['code'],
                /*'name'        => str_replace('||','<br/>',$result['name']),*/
                'product_name' => str_replace('||',' ', $product_detail['product_name']),
                'description' => html_entity_decode($product_detail['description'], ENT_QUOTES, 'UTF-8'),
                'image' => $image,
                'price' => $this->currency->format($this->tax->calculate($product_detail['price'], 0, 0)),
                'quantity' => $product_detail['quantity'],
                'update' => $this->url->link('giftregistry/registrymanage/viewRegistryDetail'),
                'remove' => $this->url->link('giftregistry/registrymanage/viewregistrydetail', '&remove=' . $product_detail['product_id'] . '&code=' . $product_detail['code']),
            );

            $this->session->data['cart']= array(
                $product_detail['product_id'].'::' => $product_detail['quantity']
            );

        }

        fprintr($this->session->data);

Here's my array sample: 这是我的数组示例:

This is the initial load of my products from the database 这是数据库中我产品的初始负载

Array
(
    [0] => Array
        (
            [product_id] => 56
            [product_name] => Luminarc Rhodes Ice Bucket
            [price] => 300.00
            [quantity] => 1
            [code] => S6-214347
            [description] => <p>Luminarc Rhodes Ice Bucket</p>

            [image] => data/metro/products/Wine and Dine/08.jpg
        )

    [1] => Array
        (
            [product_id] => 57
            [product_name] => Regent Cheese Board with Knife
            [price] => 2949.00
            [quantity] => 1
            [code] => S6-214347
            [description] => <p>Regent Cheese Board with Knife</p>

            [image] => data/metro/products/Wine and Dine/02.jpg
        )

)

Here's my session cart after updating 这是我更新后的会话购物车

[cart] => Array
        (
            [57::] => 1
        )

As you can see the last product id is stored. 如您所见,存储了最后一个产品ID。 How can I save all the product IDs? 如何保存所有产品ID?

Use this 用这个

$this->session->data['cart'][] = array(
        $product_detail['product_id'].'::' => $product_detail['quantity']
);

This will be resolve your problem. 这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM