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






Applying Functions to Array Elements Using array_map()

 
<?php

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

  function calc_sqrt($num)
  {
    $i = $num < 0 ? 'i' : '';
    return sqrt( abs($num) ) . $i;
  }

  $values = array(3, 8, -3, 0, 14, -4);

  $roots = array_map('calc_sqrt', $values);

  print '<p>Values:</p>';
  array_display($values, TRUE);

  print '<p>Square roots:</p>';
  array_display($roots, TRUE);
?>
  
  








Related examples in the same category

1.Using array_map() and array_walk() together
2.Using the array_map() Function