Quick Reference
The PHP array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
<?php
function my_function($number) {
return($number * 3.14);
}
$a = array(1, 2, 3, 4, 5);
print_r(array_map('my_function', $a));
?>
Note
Arrays count starting from zero NOT one. So item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on.
Output
Array ( [0] => 3.14 [1] => 6.28 [2] => 9.42 [3] => 12.56 [4] => 15.7 )
Syntax
array_map(my_function, array1, array2, array3, ...)
Parameters
Parameter | Description |
---|---|
my_function | The name of the user-made function, or null (required) |
array1 | Specifies an array (required) |
array2 | Specifies an array |
array3, ... | Specifies additional arrays |
PHP Notes:
- When using PHP, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
- Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.