This article mainly introduces the method of doubling the value (number) of each unit in a multi-dimensional array in PHP. It involves the skills of operating arrays in PHP. Friends who need it can refer to it.
The examples in this article describe PHP A method to double the value (number) of each cell in a multidimensional array. 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 to double the minimum unit 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); ?>
Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.
Related recommendations:
Usage of php general image processing class
PHP method of calculating the start and end dates of a week
php method to implement client-side and server-side uploading of images
The above is the detailed content of Double the value of each cell in php multidimensional array. For more information, please follow other related articles on the PHP Chinese website!