fork download
  1. <?php
  2.  
  3. $arr = array(
  4. 'chave1' => 'valor1',
  5. 'chave2' => array(
  6. 'chave3' => 'valor2',
  7. 'chave4' => array(
  8. 'chave5' => 'valor3'
  9. )
  10. ));
  11.  
  12. function setar_valor($path, $str, &$array){
  13. $niveis = explode('.', $path);
  14. $temp = &$array;
  15. foreach ( $niveis as $key ) {
  16. $temp = &$temp[$key];
  17. }
  18. $temp = $str;
  19. }
  20.  
  21. setar_valor('chave2.chave4.chave5', 'meu novo valor', $arr);
  22. var_dump($arr);
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
array(2) {
  ["chave1"]=>
  string(6) "valor1"
  ["chave2"]=>
  array(2) {
    ["chave3"]=>
    string(6) "valor2"
    ["chave4"]=>
    array(1) {
      ["chave5"]=>
      string(14) "meu novo valor"
    }
  }
}