What's the correct way to automate manipulation of a php config file of this format?
sample.php:<?phpreturn ['someobject' => ['somefield' => 'somevalue' ]];
I was thinking of converting it to JSON (json_encode), manipulate the JSON and writing it back afterwards (json_decode). But the last step gives me a different data format.
# !/bin/bash# load data to jsonJSON=$(php -r "echo json_encode(include 'sample.php');")# manipulate json with jqNEW_JSON=$(echo $JSON | jq -cr '.someobject.somefield = "othervalue"')# write backphp -r "var_dump(json_decode('$NEW_JSON', true));" > sample2.php
The content I'm getting from json_decode is:
array(1) { ["someobject"]=> array(1) { ["somefield"]=> string(10) "othervalue" }}# or without true in json_decodeobject(stdClass)#2 (1) { ["someobject"]=> object(stdClass)#1 (1) { ["somefield"]=> string(10) "othervalue" }}
What's the correct way of manipulating this type of config files?