php array_walk 与 array_map 与 foreach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10458660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:13:17  来源:igfitidea点击:

array_walk vs array_map vs foreach

phparraysforeach

提问by Jürgen Paul

I'm trying to compare these three but it seems only array_mapworks.

我试图比较这三个,但它似乎只array_map有效。

$input = array( '  hello   ','whsdf                                    ','                              lve you','                              ');
$input2 = array( '  hello   ','whsdf                                    ','                              lve you','                              ');
$input3 = array( '  hello   ','whsdf                                    ','                              lve you','                              ');

$time_start = microtime(true);
$input = array_map('trim',$input);
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did array_map in $time seconds<br>";

foreach($input as $in){
    echo "'$in': ".strlen($in)."<br>";
}

////////////////////////////////////////////////

$time_start = microtime(true);
array_walk($input2,'trim');
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did array_walk in $time seconds<br>";

foreach($input2 as $in){
    echo "'$in': ".strlen($in)."<br>";
}

////////////////////////////////////////////////


$time_start = microtime(true);
foreach($input3 as $in){
    $in = trim($in);
}
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did foreach in $time seconds<br>";

foreach($input3 as $in){
    echo "'$in': ".strlen($in)."<br>";
}

What Am I Doing Wrong? Here's the output:

我究竟做错了什么?这是输出:

Did array_map in 0.00018000602722168 seconds
'hello': 5
'whsdf': 5
'lve you': 7
'': 0
Did array_walk in 0.00014209747314453 seconds
' hello ': 10
'whsdf ': 41
' lve you': 37
' ': 30
Did foreach in 0.00012993812561035 seconds
' hello ': 10
'whsdf ': 41
' lve you': 37
' ': 30

It's not trimming for array_walkand the foreachloop.

它不是修剪 forarray_walkforeach循环。

采纳答案by Slava

array_walkdoesn't look at what result function gives. Instead it passes callback a reference to item value. So your code for it to work needs to be

array_walk不看结果函数给出什么。相反,它传递回调对项目值的引用。所以你的代码需要工作

function walk_trim(&$value) {
    $value = trim($value);
}

foreachdoesn't store changed values itself either. Change it to

foreach也不存储更改的值本身。将其更改为

foreach ($input3 as &$in) {
    $in = trim($in);
}

Read more about references.

阅读有关参考资料的更多信息

回答by Valentin Rusk

As of PHP 5.3Anonymous Functions possible. ex:

PHP 5.3 开始,匿名函数成为可能。前任:

$arr = array('1<br/>','<a href="#">2</a>','<p>3</p>','<span>4</span>','<div>5</div>');
array_walk($arr, function(&$arg){
    $arg = strip_tags($arg);
});
var_dump($arr); // 1,2,3,4,5 ;-)

Have fun.

玩得开心。