So, if you were wondering how to use this where key and value are passed in to the function. I've had success with the following (this example generates formatted html attributes from an associative array of attribute => value pairs):
<?php
$attribs = [
'name' => 'first_name',
'value' => 'Edward'
];
$formatted_attribs = array_reduce(
array_keys($attribs), function ($carry, $key) use ($attribs) { return $carry . ' ' . $key . '="' . htmlspecialchars( $attribs[$key] ) . '"';
},
''
);
echo $formatted_attribs;
?>
This will output:
name="first_name" value="Edward"