0

I have an array in PHP:

$pbx01_connection = array("customer/voip_extensions.php");

how can i add a prefix and suffix to each item in the array?

for example,

$pbx01_connection = '/admin/'.array("customer/voip_extensions.php");

so /admin/ is added before each item in the array?

charlie
  • 1,356
  • 7
  • 38
  • 76
  • http://stackoverflow.com/questions/7617639/add-a-prefix-to-each-item-of-a-php-array see the link. – johnny Sep 09 '13 at 14:56
  • check this out. http://stackoverflow.com/questions/7617639/add-a-prefix-to-each-item-of-a-php-array – johnny Sep 09 '13 at 14:57

1 Answers1

3

Use array_map():

<?php
function addPrefix($value)
{
    return '/admin/' . $value
}

$new_array = array_map("addPrefix", $array);
print_r($new_array);
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Looks like a overhead to do such a simple thing. Why not just `foreach ($array as &$value) $value = '-'.$value;` – Itay Sep 09 '13 at 14:53
  • 1
    Modifying values by reference is a bad practice as it can make following data throughout code more difficult. – John Conde Sep 09 '13 at 14:55
  • @JohnConde It can still be achieved with a simple loop. – Boaz Sep 09 '13 at 14:56
  • I don't see this as really overly complicated. Especially since this also allows the separation of logic and provides for easily organized code. – John Conde Sep 09 '13 at 14:57
  • @JohnConde I agree with you. But it's not related to "modifying values by reference". – Boaz Sep 09 '13 at 15:00