Created
March 17, 2012 15:57
-
-
Save varemenos/2061446 to your computer and use it in GitHub Desktop.
PHP - Array Map Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | |
$new_a = array_map('square', $a); | |
var_dump($a, $new_a); | |
function square($temp){ | |
return ($temp * $temp); | |
} | |
// RESULTS | |
/* | |
BEFORE: | |
array | |
0 => int 1 | |
1 => int 2 | |
2 => int 3 | |
3 => int 4 | |
4 => int 5 | |
5 => int 6 | |
6 => int 7 | |
7 => int 8 | |
8 => int 9 | |
9 => int 10 | |
AFTER: | |
array | |
0 => int 1 | |
1 => int 4 | |
2 => int 9 | |
3 => int 16 | |
4 => int 25 | |
5 => int 36 | |
6 => int 49 | |
7 => int 64 | |
8 => int 81 | |
9 => int 100 | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment