php - How to change the index name of array -


i have multidimensional array,

i'm recursively changing values of array need.

it working keys not array.

but not keys array.

how can change value of 1 test "one" => "test",

$arr = array(       'one' => array(             array('something' => 'value'),             array('something2' => 'value2'),             'another' => 'anothervalue'             ),       'two' =>  array(             array('something' => 'value'),             array('something2' => 'value2'),             'another' => 'anothervalue'             )        );  function update_something(&$item, $key) {     if($key == 'one')         $item = 'test'; }  array_walk_recursive($arr, 'update_something'); 

expected array structure is

array(           'one' => 'test',           'two' =>  array(                 array('something' => 'value'),                 array('something2' => 'value2'),                 'another' => 'anothervalue'                 )            ); 

update2

$html_structure = array(     array(         'tag' => 'div',         'class' => 'lines',         array(             'tag' => 'div',             'one' => array(                 'tag' => 'div',                  array(                     'tag' => 'span',                     'style' => 'margin:10px; padding:10px',                     'key' => 'title',                 ),                 'key' => 'subject',             )         )     ) ); 

update3

$array = array(     array(         'tag' => 'div',         'class' => 'lines',         array(             'tag' => 'div',             'repeat' => array(                 'tag' => 'div',                  array(                     'tag' => 'span',                     'style' => 'margin:10px; padding:10px',                     'key' => 'title',                 ),                 'key' => 'subject',             )         )     ) );    function update_recursively($array, $key = '', $value = array()) {     //print_r($array); print_r($value);     foreach ($array $k => $v) {         if ($k === $key){              $array[$k] = $value;         }         elseif (is_array($v))             $array[$k] = update_recursively($v);     }     return $array; }  print_r(update_recursively($array, 'repeat', array('d' => 'a'))); 

if array isn't large, work:

function update_recursively($array) {     foreach ($array $k => $v) {         if ($k === 'one')             $array[$k] = 'test';         elseif (is_array($v))             $array[$k] = update_recursively($v);     }      return $array; }  $updated_arr = update_recurisvely($arr); 

but need bit careful if it's big can slow , memory intensive. note won't update old array, array_walk_recursive would, return updated version instead.

* update *

version handles update3 scenario specify key , value replace with.

function update_recursively($array, $key = '', $value = array()) {     foreach ($array $k => $v) {         if ($k === $key)             $array[$k] = $value;         elseif (is_array($v))             $array[$k] = update_recursively($v, $key, $value);     }     return $array; } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -