问题
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.
My code is:
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);
Thank you.
回答1:
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'));
That should help.
NOTE: I've tested this using CI 3.1.0 with sessions configured as the types File and then as Database.
来源:https://stackoverflow.com/questions/39848067/store-multidimension-array-in-userdata-session-codeigniter