Reputation: 331
I try to add data to a json file.
First try:
$filename = "$root/nachrichten/bla.json";
$neueartikel = json_decode(file_get_contents($filename), true); /*until here everything works, I can loop the file*/
$neueartikel[] = array('titel' => 'bla',
'bild' => 'bla',
'url' => 'bla',
'ID' => 'bla',
'bildserver' => 'bla');
file_put_contents('$root/nachrichten/bla.json', json_encode($neueartikel);
Second try:
$filename = "$root/nachrichten/bla.json";
$neueartikel = json_decode(file_get_contents($filename), true);
$neu[] = array('titel' => 'bla',
'bild' => 'bla',
'url' => 'bla',
'ID' => 'bla',
'bildserver' => 'bla');
$result = array_merge($neueartikel, $neu);
file_put_contents('$root/nachrichten/bla.json', json_encode($result);
How can I do this right????
UPDATE:
I also tryed what PotatoIng sayd:
$filename = "$root/nachrichten/bla.json";
$temparray = json_decode(file_get_contents($filename), true);
$neu = array('titel' => 'bla',
'bild' => 'bla',
'url' => 'bla',
'ID' => 'bla',
'bildserver' => 'bla');
array_push($temparray, $neu);
file_put_contents("$root/nachrichten/bla.json", json_encode($temparray);
Still nothing (ignore this text I need to add some or else I can't submit -.-)
Upvotes: 0
Views: 159
Reputation: 33
$data[] = $_POST['data'];
$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData;
answered on Append data to a .JSON file with PHP by "Tim"
Upvotes: 0
Reputation: 360572
Basic PHP: '
-quoted strings do NOT interpolate variables:
file_put_contents('$root/nachrichten/bla.json', json_encode($neueartikel);
^^^---
Your code is trying to write your file out to a directory whose name is literally $
, r
, o
, etc...
Use "
-quoted strings instead.
Upvotes: 2