fork download
  1. <?php
  2. $seuArray = array(
  3. 'a' => 0,
  4. 'b' => array( 'b1' => 5 ,
  5. 'b2' => array( 'b2_0' => 4 ) ,
  6. 'b3' => array( 'b3_0' => 5 )
  7. ),
  8. 'c' => 2,
  9. 'd' => 5
  10. );
  11.  
  12. $saida = replaceValue($seuArray, 'b2_0', 'novo_valor');
  13.  
  14.  
  15. function replaceValue($array, $key, $newValue)
  16. {
  17.  
  18. $params = array(
  19. 'newvalue' => $newValue,
  20. 'key' => $key
  21. );
  22.  
  23. array_walk_recursive($array, function(&$v, $k) use ($params) {
  24.  
  25. if ($k == $params['key']) {
  26. $v = $params['newvalue'];
  27. }
  28. });
  29. return $array;
  30. }
  31.  
  32. print_r($saida);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [a] => 0
    [b] => Array
        (
            [b1] => 5
            [b2] => Array
                (
                    [b2_0] => novo_valor
                )

            [b3] => Array
                (
                    [b3_0] => 5
                )

        )

    [c] => 2
    [d] => 5
)