Skip to content
Advertisement

PHP Replacing element in json

I have this code:

With this code i load a local json file and attempt to array_replace, replacing if it exists or adding if it doesn’t

JavaScript

let’s say the existing json is something like this:

JavaScript

status.php?myserver[state]=10 and would result in this:

JavaScript

But then i would like to ADD to the myserver element like this: status.php?myserver[mem]=3 and would ADD to the array if it exists, like this:

JavaScript

But my code replaces the whole myserver array..

thanks!

Advertisement

Answer

Use array_merge_recursive().

JavaScript

Result:

JavaScript

https://3v4l.org/i1YvC

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement