php 开发经常遇到数组处理,会涉及到题目中这几个函数。这个函数功能类似,很多时候容易混淆。
array_walk:
array_walk — 使用用户自定义函数对数组中的每个元素做回调处理
1. 用户自定义的函数处理每一个元素
2. 直接修改原数组,不会创建新的数组
3. 可以传递额外的参数
Example #1 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
$fruits = array ( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
function test_alter(& $item1 , $key , $prefix )
{
$item1 = "$prefix: $item1" ;
}
function test_print( $item2 , $key )
{
echo "$key. $item2<br />\n" ;
}
echo "Before ...:\n" ;
array_walk ( $fruits , 'test_print' );
array_walk ( $fruits , 'test_alter' , 'fruit' );
echo "... and after:\n" ;
array_walk ( $fruits , 'test_print' );
?>
|
//运行结果
Before ...:
d. lemon
a. orange
b. banana
c. apple ... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple
更多信息参考官方文档:http://php.net/manual/zh/function.array-walk.php
array_map
array_map — 为数组的每个元素应用回调函数
1. 回调函数,应用到每个数组里的每个元素
2. 可以处理多个数组,合并处理
3. 返回新建的数组
4. 传入 NULL 作为回调函数的名称,将创建多维数组
5. 传入两个及以上的数组时,它们元素数量将会相同。因为回调函数会并行地处理相互对应的元素。 如果几个数组的元素数量不一致:空元素会扩展短那个数组,直到长度和最长的数组一样。
Example #1:
1
2
3
4
5
6
7
8
9
10
|
<?php
function 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:
1
2
3
4
5
6
7
|
<?php
$func = function ( $value ) {
return $value * 2;
};
print_r( array_map ( $func , range(1, 5)));
?>
|
//运行结果
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10)
Example #3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
function 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
)
更多信息参考官方文档:http://php.net/manual/zh/function.array-map.php
array_filter
* array_filter — 用回调函数过滤数组中的单元*
1. 依次将 array 数组中的每个值传递到 callback 函数。如果 callback 函数返回 true,则 array 数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。
2. 如果没有提供 callback 函数, 将删除 array 中所有等值为 FALSE 的条目。
3. ARRAY_FILTER_USE_KEY - callback接受键名作为的唯一参数,ARRAY_FILTER_USE_BOTH - callback同时接受键名和键值
Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
function odd( $var )
{
// returns whether the input integer is odd
return ( $var & 1);
}
function even( $var )
{
// returns whether the input integer is even
return (!( $var & 1));
}
$array1 = array ( "a" =>1, "b" =>2, "c" =>3, "d" =>4, "e" =>5);
$array2 = array (6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n" ;
print_r( array_filter ( $array1 , "odd" ));
echo "Even:\n" ;
print_r( array_filter ( $array2 , "even" ));
?>
|
//运行结果
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
Example #2:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$entry = array (
0 => 'foo' ,
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r( array_filter ( $entry ));
?>
|
//运行结果
Array
(
[0] => foo
[2] => -1
)
Example #3:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
var_dump( array_filter ( $arr , function ( $k ) {
return $k == 'b' ;
}, ARRAY_FILTER_USE_KEY));
var_dump( array_filter ( $arr , function ( $v , $k ) {
return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
?>
|
//运行结果
array(1) {
["b"]=>
int(2)
}
array(2) {
["b"]=>
int(2)
["d"]=>
int(4)
)
更多信息参考官方文档:http://php.net/manual/zh/function.array-filter.php
参考其他链接: http://www.zzvips.com/article/207228.html
到此这篇关于php array_walk array_map array_filter区别案例详解的文章就介绍到这了,更多相关php array_walk array_map array_filter区别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://hufeng.blog.csdn.net/article/details/81149885