forked from mauricemach/php-ruby-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_map.markdown.unfinished
More file actions
44 lines (36 loc) · 1.09 KB
/
Copy patharray_map.markdown.unfinished
File metadata and controls
44 lines (36 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# array_map
{{code:php
// user defined function
function mapLength($elt) {
return strlen($elt);
}
$a = array('red', 'orange', 'blue');
$result = array_map('mapLength', $a);
var_export($result);
// => array(0 => 3, 1 => 6, 2 => 4)
function mapCountry($k, $v) {
return array($k => $v);
}
// multiple arrays
$a = array('Lisbon', 'Toronto', 'New York');
$b = array('Portugal', 'Canada', 'USA');
$result = array_map('mapCountry', $a, $b);
var_export($result);
// => array(0 => array('Lisbon' => 'Portugal'),
// 1 => array('Toronto' => 'Canada'),
// 2 => array('New York' => 'USA'))
// default behavior using multiple arrays
$result = array_map(null, $a, $b);
var_export($result);
// => array(0 => array(0 => 'Lisbon', 1 => 'Portugal'),
// 1 => array(0 => 'Toronto' 1 => 'Canada'),
// 2 => array(0 => 'New York', 1 => 'USA'))
}}
{{code:ruby
}}
{{related:
array/array_filter
array/array_reduce
array/array_walk
function/create_function
}}