Home  >  Article  >  Backend Development  >  PHP method to double the value (number) of each cell in a multi-dimensional array, multi-dimensional doubling_PHP tutorial

PHP method to double the value (number) of each cell in a multi-dimensional array, multi-dimensional doubling_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:25953browse

php method to double the value (number) of each cell in a multi-dimensional array, multi-dimensional doubling

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

<&#63;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));
&#63;>

Use the functions provided by the system to solve the problem. The method is as follows:

<&#63;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);
&#63;>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/959118.htmlTechArticlephp method to double the value (number) of each unit in a multi-dimensional array, multi-dimensional doubling This article describes the example PHP method to double the value (number) of each cell in a multi-dimensional array. Share it with everyone...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn