I'm trying to replace values from a json file with values submitted from a form. Json file is as following:
{
"meta_titles": {
"Just another site": "",
"Test test": "",
"This is a test post": "",
"Hello world!": ""
},
"tag_titles": {
"Title Test": "",
"Recent Posts": "",
"Recent Comments": "",
}
}
And PHP array:
Array
(
[meta_titles-0] => Juste un autre site Wordpress
[meta_titles-1] =>
[meta_titles-2] => Ceci est un test post
[meta_titles-3] => Salut le monde
[tag_titles-0] =>
[tag_titles-1] =>
[tag_titles-2] =>
)
Should return:
{
"meta_titles": {
"Just another site": "Juste un autre site Wordpress",
"Test test": "",
"This is a test post": "Ceci est un test post",
"Hello world!": "Salut le monde"
},
"tag_titles": {
"Title Test": "",
"Recent Posts": "",
"Recent Comments": "",
}
}
What I have so far:
$filecontent = file_get_contents($website_directory.'/'.$file_to_read);
$oJson = json_decode($filecontent, true);
foreach ($_POST as $key => $val) {
foreach($oJson->fields as $i => $oVal) {
$oJson->fields[$i]->value = $val;
}
}
$json = json_encode($oJson);
var_dump($json);
Tried a lot of things but didn't find a way to do it. EDIT: I get the exact same content of the json file from var_dump.
var_dump($json);?