Using a user function to change the values of an array with array_map in PHP

Description

The following code shows how to using a user function to change the values of an array with array_map.

Example


//from   ww w  .j  av a2  s  .c  o  m
<?php
function myfunction($v){
   if ($v==="Dog"){
     return "bark";
   }
   return $v;
}

$a=array("Horse","Dog","Cat");
print_r(array_map("myfunction",$a));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone