Answers for "php foreach in heredoc"

PHP
0

php foreach in heredoc

$arr['John'] = 20;
$arr['Peter'] = 30;

$foreach = function ($array, $param) {
	$result = '';
	foreach ($array as $key => $value) {
		$search = ["{key}", "{value}"];
		$replace = [$key, $value];
		$result .= str_replace($search, $replace, $param);
	}

	return $result;
};

echo <<<HTML
	<ul>
		{$foreach($arr, '<li>{key} is {value} years old</li>')}
	<ul>
HTML;
Posted by: Guest on September-17-2021

Browse Popular Code Answers by Language