I would like to wrap all elements of an array with something like but i don't want a lot of lines or foreach loop
$links = array('london','new york','paris');
// the outcome should be
<a href="#london">london</a>
<a href="#new york">new york</a>
<a href="#paris">paris</a>
How about array_map?
$links = array('london', 'new york', 'paris');
$wrapped = array_map(
function ($el) {
return "<a href=\"#{$el}\">{$el}</a>";
},
$links
);
Demo (Click source)
Without PHP > 5.3, you can't use a lambda function, so you'll need something like this:
function wrap_those_links($el) {
return "<a href=\"#{$el}\">{$el}</a>";
}
$links = array('london', 'new york', 'paris');
$wrapped = array_map('wrap_those_links', $links);
Demo for PHP 5.2 (Again, click Source)
Try join('\n', array_map(function($a) { return "<a href=\"#$a\",>$a<\\a>";}, $links));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With