This article mainly introduces how to double the value (number) of each cell in a multi-dimensional array in php The method of doubling involves the skills of operating arrays in PHP. Friends who need it can refer to it
The example in this article describes the method of doubling the value (number) of each cell in a multi-dimensional array in PHP. Share it with everyone for your reference. The specific analysis is as follows:
Premise: A multi-dimensional array, each of its smallest unit values is a number.
Requirement: Write a function that doubles the smallest cell value.
The code is as follows
?
3 4
5
6
9 10 11 |
$arr = array(1,3,'a'=>20,'b'=>array(2,4,6,'c'=>7));
function arr2($arr){
foreach($arr as $key=>$v){
if(!is_array($v)){
$arr[$key] *= 2;
}else{
$arr[$key] = arr2($arr[$key]);
}
}
return $arr;
}
echo ""; print_r(arr2($arr)); ?> |
1 2 3 4 5 6 7 8 9 |
<🎜>$arr = array(1,3,'a'=>20,'b'=>array(2,4,6,'c'=>7));
function t(&$arr){
$arr *= 2;
}
echo ""; array_walk_recursive($arr,'t'); print_r($arr); ?> |