将多维数组存储在userdata会话codeigniter中 [英] Store multidimension array in userdata session codeigniter

查看:68
本文介绍了将多维数组存储在userdata会话codeigniter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在codeigniter userdata会话中存储多维数组。
,当我存储一个简单的数组,它工作正常。但在多维中,它什么也不存储。有没有一种方法可以在会话中存储多维数组。

I want to store multi dimension array in codeigniter userdata session. when i store a simple array it works fine. but in multi dimension it store nothing. Is there a way to store multi dimension array in session.

我的代码是:

                foreach ($unique_data as $unique_type) {
                $indexes = index_unique_values($product_all_data, 'type', $unique_type['type']);
                foreach ($indexes as $key) {
                    $product_name = $product_all_data[$key]['name'];
                    $product_type = $product_all_data[$key]['type'];
                    $product_status = $product_all_data[$key]['status'];
                    $cost = $product_all_data[$key]['cost'];
                    $price = $product_all_data[$key]['price'];
                    $barcode = $product_all_data[$key]['barcode'];

                    $product_type_all_prod[] = array('name' => $product_name, 'type' => $product_type, 'status' => $product_status, 'cost' => $cost, 'price' => $price, 'barcode' => $barcode, 'cat_name' => '');
                }
            }

            $product_bytype_array = array("product_by_type" => $product_type_all_prod);
            $this->session->set_userdata($product_bytype_array);

谢谢。

推荐答案

如果您尝试类似的操作……

What if you try something like...

$this->session->set_userdata(['product_data' =>$product_bytype_array]);

您可以检查自己是否确实在以期望的格式生成数组。

You can check that you are actually generating an array in the format you are expecting.

然后检查存储的结果是否符合您期望的格式。

Then check that the Stored result is in the format you are expecting.

// What's in the resulting array that we want to store in the session?
var_dump($product_type_all_prod);  

$product_bytype_array = array("product_by_type" => $product_type_all_prod);
$this->session->set_userdata($product_bytype_array);

//What's in the array in the session
var_dump($this->session->userdata('product_by_type'));

应该有帮助。

注意:我已经使用CI 3.1.0进行了测试,并将会话配置为文件类型,然后配置为数据库。

NOTE: I've tested this using CI 3.1.0 with sessions configured as the types File and then as Database.

这篇关于将多维数组存储在userdata会话codeigniter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆