Home > Backend Development > PHP Tutorial > 关于array_walk的问题

关于array_walk的问题

WBOY
Release: 2016-06-23 14:38:55
Original
830 people have browsed it

$fruits = array("d"=>"lemon","a"=>"apple","b"=>"banana","c"=>"orange");
function test_alter(&$item1,$key,$prefix)
{
$item1 = "$prefix:$item1";
}
function test_print($item2,$key)
{
echo "$key.$item2
 \n";
}
echo "Before.....\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter','fruit');
echo "...and after:\n";
array_walk($fruits,'test_print');
?>
关于这段代码的 最后几行的遍历 表示理解不能  求大大们帮我细细的分析一下 让我理解~


回复讨论(解决方案)

array_walk($fruits, 'test_print');
用回调函数 test_print 打印数组 $fruits 的每个成员
相当于
foreach($fruits as $k=>$v) test_print($v, $k);

array_walk($fruits, 'test_alter','fruit');
用回调函数 test_alter 给数组 $fruits 的每个成员的值的前面加上字符串 'fruit.'
相当于
foreach($fruits as $k=>$v) $fruits[$k] = test_alter($v, 'fruit');

谢谢版主  学习了   ~   
这一下就懂了~

PHP array_walk() 函数
直接看例句,应该看得懂的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template