0

My data source is this JSON that I json_decode($origJason,true) to the array $j1.

I have no control over the JSON that I get.

{
"A": {
    "This": "AAA"
},
"B": {
    "That": ["CCC", "DDD"]
}

}

The array looks like this

Array
(
    [A] => Array
        (
            [This] => AAA
        )

    [B] => Array
        (
            [That] => Array
                (
                    [0] => CCC
                    [1] => DDD
                )

        )

)

I want to replace the value for the key "This" with "ZZZ"

function array_replacing(&$item, $key){

    if($key == 'This')
    $item = 'ZZZ';
    
}

   array_walk_recursive($j1, 'array_replacing');

The function also replaces the first value in the "That" array.

Array
(
    [A] => Array
        (
            [This] => ZZZ
        )
    [B] => Array
        (
            [That] => Array
                (
                    [0] => ZZZ
                    [1] => DDD
                )
        )
)

When I tried json_decode($origJason,true) I get this problem.

When I tried json_decode($origJason,false) there is no replacement.

I have no idea what's going on here and how to fix that.

Will be grateful for a solution.

Thanks!

Hanan Cohen
  • 383
  • 2
  • 15

0 Answers0