Proper way to get array from session - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Proper way to get array from session (/showthread.php?tid=79080) |
Proper way to get array from session - sfarzoso - 04-17-2021 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? RE: Proper way to get array from session - InsiteFX - 04-17-2021 I do not believe that sessions work with the dot notation on session arrays. RE: Proper way to get array from session - iRedds - 04-17-2021 PHP Code: session('error.meta.featured_image') RE: Proper way to get array from session - sfarzoso - 04-18-2021 (04-17-2021, 09:26 PM)iRedds Wrote: I already tried with the first one and I get NULL how is that possible? ? RE: Proper way to get array from session - iRedds - 04-18-2021 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' => '...']] RE: Proper way to get array from session - sfarzoso - 04-18-2021 (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? RE: Proper way to get array from session - iRedds - 04-20-2021 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 |