谁能帮我解释一下这段代码 PHP
<?
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
function test_alter( $item1 ) {
$item1 = 'bogus';
}
function test_print( $item2 ) {
echo "$item2<br>\n";
}
array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter' );
array_walk( $fruits, 'test_print' );
?>
答案:<?
//定义一个数组¥fruits
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
//定义一个方法test_alter 将传入参数重新赋值
function test_alter( $item1 ) {
$item1 = 'bogus';
}
//定义一个方法est_print将传入参数输出
function test_print( $item2 ) {
echo "$item2<br>\n";
}
/*
array_walk(‘数组’,’方法‘)方法 会遍历整个数组而不管指针的位置
*/
array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter' );
//这里输出: d. lemon a. orange b. banana c. apple
array_walk( $fruits, 'test_print' );
//这里输出:d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple
?>
上一个:php中如何取得服务器操作系统的默认编码方式?
下一个:学编程的哥们给个建议PHP还是.NET