CodeIgniter Forums
Parser for Multi-dimensional Array with the same key - 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: Parser for Multi-dimensional Array with the same key (/showthread.php?tid=78928)



Parser for Multi-dimensional Array with the same key - emmanuel - 03-28-2021

Consider the followings:
PHP Code:
$template 'Hello {name}, your partner is {partner}{name}{/partner}.';
$data = [
    'name' => 'John',
    'partner' => [
        ['name' => 'Mary'],
    ]
];

$parser = \Config\Services::parser();
echo 
$parser->setData($data)->renderString($template); 


It renders the following output:
Quote:Hello John, your partner is John.


Just wondering how do you access partner's name while it has the same key name. I have tried dot notation , but it seems it doesn't support that.


RE: Parser for Multi-dimensional Array with the same key - craig - 03-29-2021

Quote:The Parser class uses an associative array internally, to accumulate pseudo-variable settings until you call its render(). This means that your pseudo-variable names need to be unique, or a later parameter setting will over-ride an earlier one.

https://codeigniter.com/user_guide/outgoing/view_parser.html

You will need to make your keys unique.

Code:
$data = [
    'person_name' => 'John',
    'partner' => [
        ['partner_name' => 'Mary'],
    ]
];



RE: Parser for Multi-dimensional Array with the same key - emmanuel - 03-29-2021

Thanks Craig, missed that paragraph. Haha.