-1

I require a json output in the sample format:

  [  { "date": "2013-04-01 17:30:00", "type": "meeting", "title": "Test Last Year",    "description": "Lorem Ipsum dolor set", "url": "http://test.com?id=1" },  { "date": "2013-04-02 17:30:00",    "type": "meeting", "title": "Test Last Year", "description": "Lorem Ipsum dolor set", "url":   "http://test.com?id=2" }   }]

The last comma needs to be stripped out after : "http://test.com?id=2" } , }] to create a json out put without error. I am using php and here is the relevant portion of my php code: (slightly changed to display only relevant portion)

echo '[';
foreach ($enquirydates as $edate) 
{

 if ($x <= $firstunixdate )//first follow-up
 {
  echo '  { "date": "'; echo date("Y-m-d ", $firstunixdate); echo '", "type": "Follow-up", "title": "Student Enquiry ';  echo ' Follow-up (First Follow-Up)", "description": "First Student Enquiry followup due..", "url": "http://test.com/admin/followup.php?time=';echo $unixenquirydate; echo '" },';
 }
elseif ($x <= $secondunixdate)//second followup
{
 echo '  { "date": "'; echo date("Y-m-d ",$secondunixdate); echo '", "type": "Follow-up", "title": "Student Enquiry ';  echo ' Follow-up (Second Follow-Up)", "description": "Second Student Enquiry followup due..", "url": "http://test.com/admin/followup.php?time=';echo $unixenquirydate; echo '"},';
 }
elseif ($x > $finalunixdate)//enquiry open for more than 25 days
{
 echo '  { "date": "'; echo date("Y-m-d ",$x); echo '", "type": "Follow-up", "title": "Student Enquiry ';  echo ' open for more than 25 days", "description": "Second Student Enquiry followup due..", "url": "http://test.com/admin/followup.php?time=';echo $unixenquirydate; echo '"},';
}//end if
}  

echo ']';

I need to remove the last comma in the foreach loop. I know that implode can be used to remove ending strings in php. But I am lost on how to use it in this context (multiple echo statements)

Requesting help..

shels
  • 21
  • 2
  • 9

2 Answers2

3

This is not the way to generate a JSON.

Create an array and use json_encode() function to convert it into a JSON string.

But if you insist on doing this your way:

$json = Array();
foreach($enquirydates as $edate) {
    if($x <= $firstunixdate) { //first follow-up
        $json[] = '{"date":"'.date("Y-m-d", $firstunixdate).'","type":"Follow-up","title":"Student Enquiry Follow-up (First Follow-Up)","description":"First Student Enquiry followup due..","url":"http://test.com/admin/followup.php?time='.$unixenquirydate.'"}';
    }
    else if($x <= $secondunixdate) { //second followup
        $json[] = '{"date":"'.date("Y-m-d",$secondunixdate).'","type":"Follow-up","title":"Student Enquiry Follow-up (Second Follow-Up)","description":"Second Student Enquiry followup due..","url":"http://test.com/admin/followup.php?time='.$unixenquirydate.'"}';
    }
    else if($x > $finalunixdate) { //enquiry open for more than 25 days
        $json[] = '{"date":"'.date("Y-m-d",$x).'","type":"Follow-up","title":"Student Enquiry open for more than 25 days","description":"Second Student Enquiry followup due..","url":"http://test.com/admin/followup.php?time='.$unixenquirydate.'"}';
    } //end if
}  
echo '['.implode(',', $json).']';
transilvlad
  • 13,974
  • 13
  • 45
  • 80
  • I am getting the required output which includes the last comma..I need to remove comma...How to use implode in such a situation , without considering the json part.. – shels Dec 01 '13 at 02:54
  • Sorry, not getting required output: [2013-12-01 ", "type": "Follow-up", "title": "Student Enquiry open for more than 25 days", "description": "Second Student Enquiry followup due..", "url": "http://test.com/admin/followup.php?time=1382207400"},2013-12-01 ", "type": "Follow-up", "title": "Student Enquiry Follow-up (First Follow-Up)", "description": "First Student Enquiry followup due..", "url": "http://test.com/admin/followup.php?time=1385231400" },[ { "date": ", { "date": "] – shels Dec 01 '13 at 03:06
  • Comma still remains and towards the end ,[ { "date": ", { "date": "] – shels Dec 01 '13 at 03:15
  • It is not working ..the Date portion is getting stripped from begining and placed towrds the end as follows, along with comma: ,[ { "date": ", { "date": "] – shels Dec 01 '13 at 03:34
2

Instead of building the JSON by hand you should build an array first, and use $json = json_encode($array) to get the valid JSON string.

bagonyi
  • 3,258
  • 2
  • 22
  • 20
  • I am getting the required output which includes the last comma..I need to remove comma...How to use implode in such a situation , without considering the json part. – shels Dec 01 '13 at 03:35
  • `echo '[' . rtrim(implode(',', $json), ',') . ']';` – bagonyi Dec 01 '13 at 03:38
  • I do not need the [ ] part as i have already hard coded it earlier. So I tried echo 'rtrim(implode(',', $json), ','); Still getting , { "date": ", { "date": "] towards the end...Comma is still there and date has been shifted towards the end ??? – shels Dec 01 '13 at 03:46