0

I'm attempting to apply a function to an array of animals. I want to embolden them.

$arr = array('cat', 'dog');

function makemebold($item)
{
    return "<b>$item</b>"; // or something more interesting... 
}   

Let's check out the original array:

// dump original array
var_dump($arr); echo '<br>';

Returns:

array(2) { [0]=> string(3) "cat" [1]=> string(3) "dog" } 

Now, let's apply array_map:

array_map($arr, 'makemebold');
var_dump($arr); echo '<br>';

Nothing doing:

array(2) { [0]=> string(3) "cat" [1]=> string(3) "dog" } 

Now, array_walk:

array_walk($arr, 'makemebold');
var_dump($arr); echo '<br>';

Same as above - no change:

array(2) { [0]=> string(3) "cat" [1]=> string(3) "dog" } 

What am I doing wrong?

a coder
  • 7,530
  • 20
  • 84
  • 131
  • possible duplicate of [Difference between array\_map, array\_walk and array\_filter](http://stackoverflow.com/questions/3432257/difference-between-array-map-array-walk-and-array-filter) – a coder Dec 09 '14 at 20:42

1 Answers1

1

array_map doesn't modify the input array, it returns a new array with results of calling the function on each element of the input.

$bold_arr = array_map('makemebold', $arr);
var_dump($bold_arr); echo '<br>';
Barmar
  • 741,623
  • 53
  • 500
  • 612