php - How to change the value of the current array key -
i'm trying alter values in recursive array have unknown number/depth of nested arrays. think it's syntax tripping over.
basically need output $orgarray 1 time again 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 $key => $value){ if (is_array($value)){ recursearray($value); // append keys nested array ??? }else{ // alter current key's value ??? } } } recursearray($orgarray, $modarray); echo '<pre>'; print_r($modarray); echo '</pre>'; what doing wrong here?
i unable alter value of current key
this won't output array @
edit ok - changed way function beingness called:
function recursearray($array, &$modarray){ if(!isset($modarray)) { $modarray = array(); } foreach($array $key => $value){ if (is_array($value)){ recursearray($value, &$modarray); // append keys nested array // neither of these work array_push($value['newkey'] = 'new_value'); $value['newkey'] = 'new_value'; }else{ // alter current key's value $array[$key] = 'value'; } } homecoming $array; } $modarray = recursearray($orgarray, $modarray); and it's there, still don't understand why original phone call function did not work ( recursearray($orgarray, $modarray); ) , 2 methods trying add together keys nested arrays not work either.
i think works:
<?php function recursearray(&$array){ foreach($array $key => &$value){ if (is_array($value)){ recursearray($value); }else{ $value = 'other-value'; }; }; }; ?> take atention foreach($array $key => &$value){
regards!
php arrays
No comments:
Post a Comment