Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't store multidimensional data with Session Library #2309

Closed
desttools opened this issue Oct 7, 2019 · 3 comments
Closed

Can't store multidimensional data with Session Library #2309

desttools opened this issue Oct 7, 2019 · 3 comments

Comments

@desttools
Copy link

I was trying to store a multidimensional array using Session Library, and it is failing when I try to call set(). Not sure if this is a bug or just an unimplemented feature.

My shopping cart array $cart is set up like this:

Array ( [0] => Array ( [item] => Adult [qty] => 2 [price] => 38.02 ) [1] => Array ( [item] => Child (Ages 4-12) [qty] => 2 [price] => 17.68 ) [2] => Array ( [item] => Lap Child (Ages 0-3) [qty] => 2 [price] => 0 ) [3] => Array ( [item] => Family Pass (Max 3 kids) [qty] => 1 [price] => 87.53 ) [total] => 198.93 )

And I'm trying to store it like this:

$session->set($cart);

It fails and I get this error from the debugger:

**ErrorException
Illegal offset type

SYSTEMPATH/Session\Session.php at line 469**

if (is_int($key)) { $_SESSION[$value] = null; }

I was able to work around this by doing json_encode() on the data and storing it as a name-value pair. I was also able to store it with pure PHP like $_SESSION['cart'] = $cart;

CodeIgniter 4 version
CodeIgniter 4.0.0-r2.1

@dafriend
Copy link
Contributor

$session->set($data) expects $data to be an array of key/value pairs. The key is used as the associative index to $_SESSION I believe what you want to do is something like this

$_SESSION['cart'] = $cart; 

@lonnieezell
Copy link
Member

@dafriend is correct here. That's not a bug, you're just not providing anything to index it by, which means there's no way to retrieve it.

@desttools
Copy link
Author

That makes sense. Thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants