# array_map

(PHP 4 >= 4.0.6, PHP 5)

array_map 将回调函数作用到给定数组的单元上

### 说明

array array_map ( callback \$callback , array \$arr1 [, array \$... ] )

array_map() 返回一个数组，该数组包含了 arr1 中的所有单元经过 callback 作用过之后的单元。callback 接受的参数数目应该和传递给 array_map() 函数的数组数目一致。

Example #1 array_map() 例子

``` <?phpfunction cube(\$n){    return(\$n * \$n * \$n);}\$a = array(1, 2, 3, 4, 5);\$b = array_map("cube", \$a);print_r(\$b);?> ```

```Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
```

Example #2 array_map() - 使用更多的数组

``` <?phpfunction show_Spanish(\$n, \$m){    return("The number \$n is called \$m in Spanish");}function map_Spanish(\$n, \$m){    return(array(\$n => \$m));}\$a = array(1, 2, 3, 4, 5);\$b = array("uno", "dos", "tres", "cuatro", "cinco");\$c = array_map("show_Spanish", \$a, \$b);print_r(\$c);\$d = array_map("map_Spanish", \$a , \$b);print_r(\$d);?> ```

```// printout of \$c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)

// printout of \$d
Array
(
[0] => Array
(
[1] => uno
)

[1] => Array
(
[2] => dos
)

[2] => Array
(
[3] => tres
)

[3] => Array
(
[4] => cuatro
)

[4] => Array
(
[5] => cinco
)

)
```

Example #3 建立一个数组的数组

``` <?php\$a = array(1, 2, 3, 4, 5);\$b = array("one", "two", "three", "four", "five");\$c = array("uno", "dos", "tres", "cuatro", "cinco");\$d = array_map(null, \$a, \$b, \$c);print_r(\$d);?> ```

```Array
(
[0] => Array
(
[0] => 1
[1] => one
[2] => uno
)

[1] => Array
(
[0] => 2
[1] => two
[2] => dos
)

[2] => Array
(
[0] => 3
[1] => three
[2] => tres
)

[3] => Array
(
[0] => 4
[1] => four
[2] => cuatro
)

[4] => Array
(
[0] => 5
[1] => five
[2] => cinco
)

)
```

• array
• array_change_key_case
• array_chunk
• array_combine
• array_count_values
• array_diff
• array_diff_assoc
• array_diff_key
• array_diff_uassoc
• array_diff_ukey
• array_fill
• array_fill_keys
• array_filter
• array_flip
• array_intersect
• array_intersect_assoc
• array_intersect_key
• array_intersect_uassoc
• array_intersect_ukey
• array_keys
• array_key_exists
• array_map
• array_merge
• array_merge_recursive
• array_multisort
• array_pop
• array_product
• array_push
• array_rand
• array_reduce
• array_replace
• array_replace_recursive
• array_reverse
• array_search
• array_shift
• array_slice
• array_splice
• array_sum
• array_udiff
• array_udiff_assoc
• array_udiff_uassoc
• array_uintersect
• array_uintersect_assoc
• array_uintersect_uassoc
• array_unique
• array_unshift
• array_values
• array_walk
• array_walk_recursive
• arsort
• asort
• compact
• count
• current
• each
• end
• extract
• in_array
• key
• krsort
• ksort
• list
• natcasesort
• natsort
• next
• pos
• prev
• range
• reset
• rsort
• shuffle
• sizeof
• sort
• uasort
• uksort
• usort
• PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道