Welcome Guest, Not a member yet? Register   Sign In
Invalid argument supplied for foreach() when using parser
#6

(02-19-2020, 02:34 PM)MikiStoni Wrote: the following example from the documentation sends folgwenden error: CRITICAL - 2020-02-19 14:50:57 --> Invalid argument supplied for foreach()

$template = '{name} lives in {location}{city} on {planet}{/location}.';

$data = [
        'name'    => 'George',
        'location' => [ 'city' => 'Red City', 'planet' => 'Mars' ]
];

echo $parser->setData($data)->renderString($template);
// Result: George lives in Red City on Mars.

Dear MikiStoni,

There is a typo in the documentation. Location should be enclosed in additional square brackets to form an array, e.g.
PHP Code:
$data = [
        'name'    => 'George',
        'location' => [['city' => 'Red City''planet' => 'Mars']]
]; 

This comes because Parser expects attribute to be either a string (like George), or an Array-of-Arrays (e.g. $location[0] = ['city' => 'Red City', 'planet' => 'Mars']) in order to be able to iterate over it, and then process each pair inside the outer array as normal key:value pair like it was processing 'name'=>'George' pair before.

Probably topicstarter should do the same with errors array, and enclose it into additional array.
Reply


Messages In This Thread
RE: Invalid argument supplied for foreach() when using parser - by zahhar - 02-24-2020, 02:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB