What about this shorter, more transparent, yet more intuitive with array_walk
$attributes = array(
'data-href' => 'http://example.com',
'data-width' => '300',
'data-height' => '250',
'data-type' => 'cover',
);
$args = "";
array_walk(
$attributes,
function ($item, $key) use (&$args) {
$args .= $key ." = '" . $item . "' ";
}
);
// output: 'data-href="http://example.com" data-width="300" data-height="250" data-type="cover"