Ho la seguente JSON in un file list.txt
:PHP leggere e scrivere JSON da file
{
"bgates":{"first":"Bill","last":"Gates"},
"sjobs":{"first":"Steve","last":"Jobs"}
}
Come faccio ad aggiungere "bross":{"first":"Bob","last":"Ross"}
al mio file con PHP?
Ecco quello che ho finora:
<?php
$user = "bross";
$first = "Bob";
$last = "Ross";
$file = "list.txt";
$json = json_decode(file_get_contents($file));
$json[$user] = array("first" => $first, "last" => $last);
file_put_contents($file, json_encode($json));
?>
che mi dà un errore fatale: Impossibile utilizzare oggetto di tipo stdClass come serie su questa linea:
$json[$user] = array("first" => $first, "last" => $last);
sto usando PHP5. 2. qualche idea? Grazie!
Detesto che 'json_decode' di default restituisca una classe anziché una matrice. Questo mi eccita ogni volta che uso 'json_decode' per la prima volta in un mese. – Tim