I'm trying to change all the values in a recursive array that will have an unknown number/depth of nested arrays. I think it's just syntax that I am tripping over.
Basically I need to output the $orgarray again with all new values.
$orgarray = array( '101' => 'some-value', '102' => 'some-value', '103' => 'some-value', '104' => array( '201' => 'some-value', '202' => 'some-value', '203' => array( '301' => 'some-value', '302' => array( '401' => 'some-value', '402' => 'some-value', '501' => array( '502' => 'some-value', '503' => 'some-value', '504' => 'some-value', '505' => 'some-value', '506' => 'some-vaslue' ), ), ), ), '105' => 'some-value', '106' => 'some-value', '107' => 'some-value' ); function recursearray($array, &$modarray){ foreach($array as $key => $value){ if (is_array($value)){ recursearray($value); // append keys to this nested array ??? }else{ // change current key's value ??? } } } recursearray($orgarray, $modarray); echo '<pre>'; print_r($modarray); echo '</pre>'; what am I doing wrong here?
I am unable to change the value of the current key
this won't output an array at all
EDIT ok - I changed the way the function was being called:
function recursearray($array, &$modarray){ if(!isset($modarray)) { $modarray = array(); } foreach($array as $key => $value){ if (is_array($value)){ recursearray($value, &$modarray); // append keys to this nested array // neither of these work array_push($value['newkey'] = 'new_value'); $value['newkey'] = 'new_value'; }else{ // change current key's value $array[$key] = 'value'; } } return $array; } $modarray = recursearray($orgarray, $modarray); and now it's almost there, but I still don't understand why the original call to the function did not work ( recursearray($orgarray, $modarray); ) and the 2 methods trying to add keys to the nested arrays do not work either.
$array[$key] = $new_value;.