Home > Backend Development > PHP Tutorial > Double the value of each cell in php multidimensional array

Double the value of each cell in php multidimensional array

墨辰丷
Release: 2023-03-31 22:04:02
Original
2935 people have browsed it

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,&#39;a&#39;=>20,&#39;b&#39;=>array(2,4,6,&#39;c&#39;=>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));
?>
Copy after login

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

<?php
$arr = array(1,3,&#39;a&#39;=>20,&#39;b&#39;=>array(2,4,6,&#39;c&#39;=>7));
function t(&$arr){ 
 $arr *= 2;
}
echo "<pre class="brush:php;toolbar:false">";
array_walk_recursive($arr,&#39;t&#39;);
print_r($arr);
?>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template