<?php
//用递归使数组内每个数值翻倍
$arr = array(1,2,'b',array(3,'c',array(4,5)));
function test($arr){
foreach($arr as $k=>$v){
if(is_int($v)){
$arr[$k]=$v*2;
}
elseif(is_array($v)){
$arr[$k]=test($v);
}
else{
continue;
}
}
print_r($arr);
}
test($arr);
?>
这个文件主要是将数组里面的数字变为原来的二倍,我的困惑是为啥在test函数里打印则不行呢,必须返回数组。
我想打印的数组是这样的Array ( [0] => 2 [1] => 4 [2] => b [3] => Array ( [0] => 6 [1] => c [2] => Array ( [0] => 8 [1] => 10 ) ) )
实际是Array ( [0] => 8 [1] => 10 ) Array ( [0] => 6 [1] => c [2] => ) Array ( [0] => 2 [1] => 4 [2] => b [3] => )