Applying Functions to Array Elements Using array_walk() : array_walk « Data Structure « PHP






Applying Functions to Array Elements Using array_walk()

 
<?php
  function array_display($array, $pre=FALSE)
  {
    $tag = $pre ? 'pre' : 'p';
    printf("<%s>%s</%s>\n", $tag, var_export($array, TRUE), $tag);
  }

  function modify(&$element)
  {
    $element *= 1.5;
  }

  $array = array(10, -3.5, 2, 7);
  array_display($array, TRUE);

  array_walk($array, 'modify');
  array_display($array, TRUE);
?>
  
  








Related examples in the same category

1.Apply a user function to every member of an array.
2.array_walk() function applies a function to several or all elements in an array.