title: PHP code to write a line before the last line
body: <p>For my jQuery Calendar I need to edit this line of code to insert a new event.</p>
  
  <pre><code>[
  {"id":1, "start": new Date(year, month, day, 12), "end": new Date(year, month, day, 13, 35),"title":"Lunch with Mike"},
  {"id":2, "start": new Date(year, month, day, 14), "end": new Date(year, month, day, 14, 45),"title":"Dev Meeting"},
  ]
  </code></pre>
  
  <p>file_put_contents would do an awesome job if the "]" wasn't needed at the last line, so perhaps there is a way to lookup "]" and replace it with: <br/></p>
  
  <pre><code>{"id":3, "start": new Date(year, month, day, 14), "end": new Date(year, month, day, 14, 45),"title":"New Thing"},
      ]
  </code></pre>
  
  <p>Also, my reservations can be unset, so I also need to find a way to find that specific line (search by ID) and delete it untill the next line...But how can I achieve that?
  <br/>(This code should be triggered when I'm reserving or unreserving an item.)</p>
  
  <p>Perhaps, if this is even possible, this is not the right way?<br/><br/>
  What if I set a trigger on the calendar section of the site: before the calendar view is created, the event file will be filled with all the information out of the database, saved, and the calendar then will show all the events.<br/></p>
  
  <p>But how intensive is this for the page? If the database is filled with 1000 reservations, 1002 lines has to be created over and over again every time you check the calendar page. Would this be a good idea?</p>
  
  <p>What are my options here? And which one is the best/right way?</p>
  
  <p>Thanks in advance. </p>