php - Recursively update every element of multidimensional array -


i have large multidimensional array this:

$array = [     'a' => 1,     'b' => 5,     'c' => [         'c1' => 12,         'c2' => [             'd1' = 4,             'd2' => 25         ],         'c3' => [/*...*/]     ] ]; 

the array of unknown size , dimension , trying modify each element, example add 1 every elements value.

i've been googling around , found recursive functions visit each element , print contents out, nothing modifying each element go.

the following code (that found online)

function x($a) {     if (!is_array($a)) {         echo ($a+1);         return;     }      foreach($a $v) {         x($v);     } } 

allows me print out contents of array, how modify update array elements calculation instead of echoing out?

array_walk_recursive ($array, function (&$val) {  $val += 1; }); print_r($array); 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -