[SOLVED] [JSON/PHP] Decoding then encoding with new values
Posted: Wed Mar 23, 2011 7:28 pm
I have a problem with JSON in php... I have searched everywhere to no avail...
I simply want to take current JSON data decode it in to a object modify it, then encode it in to proper JSON format.
So heres my current code which successfully decodes the JSON and adds a array value "$data[6]->clicks[$no + 1]"
How do I re-encode this so it preserves all existing data but adds data to the "clicks" array?
I simply want to take current JSON data decode it in to a object modify it, then encode it in to proper JSON format.
So heres my current code which successfully decodes the JSON and adds a array value "$data[6]->clicks[$no + 1]"
Code: Select all
$data[6] = '{
"dates": ["12-3-1996", "12-4-1996", "12-5-1996", "12-6-1996", "3-23-2011"],
"views": ["432", "322", "1", "24", "343"],
"clicks": ["32", "21", "0", "6", "6"]
}';
$data[6] = json_decode($data[6]);
$no = count($data[6]->dates) - 1;
if($data[6]->dates[$no] == date("n-j-Y")) {
$data[6]->clicks[$no] = $data[6]->clicks[$no] + 1;
} else {
$data[6]->clicks[$no + 1] = "1";
}