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
<?php $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 "<pre class="brush:php;toolbar:false">"; print_r(arr2($arr)); ?>
Use the functions provided by the system to solve the problem. The method is as follows:
<?php $arr = array(1,3,'a'=>20,'b'=>array(2,4,6,'c'=>7)); function t(&$arr){ $arr *= 2; } echo "<pre class="brush:php;toolbar:false">"; array_walk_recursive($arr,'t'); print_r($arr); ?>
I hope this article will be helpful to everyone’s PHP programming design.