Here is my PHP code:
<?php
function getItems($arg) {
$products = array();
$products[] = (object) array('id' => '0', 'title' => 'Product1');
$products[] = (object) array('id' => '1', 'title' => 'Product2');
return $products;
}
$jsonContent = '{"myKey":["loop", "getItems(\"Arg\")", "<div><h3>{$item->title}</h3></div>"]}';
$jsonObj = json_decode($jsonContent);
$options = $jsonObj->myKey;
if($options[0] == 'loop') {
$html = [];
foreach($options[1] as $item) {
$html[] = $options[2];
}
echo implode('', $html);
}
?>
Of course, I get the warning
Invalid argument supplied for foreach() ...
because the argument for the foreach() is not an array but a string.
So how to convert a string as name of a function and call the function?
And how to use the third element of the $options array as a wrapper with dynamic data from the loop (I hope you will understand me by the code)
I need to get the following result:
<div><h3>Product1</h3></div>
<div><h3>Product2</h3></div>
foreach()but on the page I see{$item->title}{$item->title}So how to use item property like$item->titleas dynamic data?