• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Proper way to get array from session

#1
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?
Reply

#2
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 )
Reply

#3
PHP Code:
session('error.meta.featured_image'
//equal
session('error')['meta']['featured_image']
// and match
['error' => ['meta' => ['featured_image' => '...']]]

// and does not match 
['error' => ['meta.featured_image' => '...']] 
Reply

#4
(04-17-2021, 09:26 PM)iRedds Wrote:
PHP Code:
session('error.meta.featured_image'
//equal
session('error')['meta']['featured_image']
// and match
['error' => ['meta' => ['featured_image' => '...']]]

// and does not match 
['error' => ['meta.featured_image' => '...']] 

I already tried with the first one and I get NULL how is that possible? ?
Reply

#5
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' => '...']]
Reply

#6
(04-18-2021, 03:20 AM)iRedds Wrote: 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' => '...']]

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?
Reply

#7
I will show you an example of how you can get out of this situation.
PHP Code:
// route 
$routes->add('/''Home::index');
// in controller 
public function index()
{
    if (
$this->request->getMethod() !== 'get') {
        if (! 
$this->validate(['a.c' => 'required'])) {
            return 
redirect()->to('/')->withInput();
        } else {
            
d('win');
        }

    } else {
        
session(); 
        return 
view('template', ['validator' => Services::validation()]);
    }
}
// template
<?php /** @var Codeigniter\Validation\Validation $validator */ ?>
<h1>error: <?= $validator->getError('a.c')?></h1>
<form method="post" action="/">
    <input type="text" name="a[b]" value="aa">
    <input type="submit">
</form> 

Anyway, I added a PR to access the key containing the dot using dot notation.
https://github.com/codeigniter4/CodeIgniter4/pull/4588
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.