Proper way to get array from session |
I have a session which contains an item called "error", this item contains an array with the following structure:
array(1) { ["meta.featured_image"]=> string(60) "Il file meta.featured_image supera le dimensioni consentite." } I'm trying to print the item featured_image, so I did this: PHP Code: <?= session('error.meta.featured_image') ?> But the code above returns NULL but if I do PHP Code: <?= session('error')['meta']['featured_image'] ?> works but I need to check if the index exists to prevent error, there is no way to get the featured_image index directly in the session?
I do not believe that sessions work with the dot notation on session arrays.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
PHP Code: session('error.meta.featured_image') (04-17-2021, 09:26 PM)iRedds Wrote: I already tried with the first one and I get NULL how is that possible? ?
This will return null because there is no matching data.
You cannot get the data using: session('error')['meta']['featured_image'] for ['error' => ['meta.featured_image' => '...']] (04-18-2021, 03:20 AM)iRedds Wrote: This will return null because there is no matching data. the input I have is this: PHP Code: <input type="file" id="featuredImage" accept=".png, .jpg, .jpeg" name="meta[featured_image]"> I guess session('error.meta.featured_image') should be correct?
I will show you an example of how you can get out of this situation.
PHP Code: // route Anyway, I added a PR to access the key containing the dot using dot notation. https://github.com/codeigniter4/CodeIgniter4/pull/4588 |
Welcome Guest, Not a member yet? Register Sign In |