[SOLVED] [JSON/PHP] Decoding then encoding with new values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

[SOLVED] [JSON/PHP] Decoding then encoding with new values

Post by Quazel »

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]"

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";
}
How do I re-encode this so it preserves all existing data but adds data to the "clicks" array?
Last edited by Quazel on Wed Mar 23, 2011 7:36 pm, edited 1 time in total.
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Re: [JSON/PHP] Decoding then encoding with new values

Post by Quazel »

Wow I feel really stupid Solved it...

Code: Select all

json_encode($data[6]);
Post Reply