I'm trying to get the following formatting in JSON using PHP which is querying the database... notice there is no final comma
[
{
"ID": "4",
"Name": "Jill Higgins",
"Job Title": "Designer",
"Organisation": "Design Widget",
"Organisation Type": "Academia",
"Latitude": "54.669978",
"Longitude": "-1.452469"
},
{
"ID": "5",
"Name": "Bob Billy",
"Job Title": "Clown",
"Organisation": "Big Comp INC",
"Organisation Type": "Company",
"Latitude": "54.669978",
"Longitude": "-1.452469"
}
]
This is my code currently...
if (PerchUtil::count($members)) {
echo '[';
foreach ($members as $Member) {
//prepare the data
$data = array(
'ID' => $Member->memberID(),
'Name' => $Member->first_name() . ' ' . $Member->last_name(),
'Job Title' => $Member->expert_job_title(),
'Organisation' => $Member->expert_org_name(),
'Organisation Type' => $Member->expert_org_type(),
'Latitude' => $Member->expert_org_latitude(),
'Longitude' => $Member->expert_org_longitude()
);
}
echo ']';
}
header('Content-Type: application/json');
This is what it currently looks like... notice there is a comma at the end which I don't need. The spacing isn't really helpful with the brackets either... how do I amend the PHP so it is cleaner and counts out the final comma?
[{
"ID": "4",
"Name": "Jill Higgins",
"Job Title": "Designer",
"Organisation": "CPI",
"Organisation Type": "Academia",
"Latitude": "54.669978",
"Longitude": "-1.452469"
},{
"ID": "5",
"Name": "Bob Billy",
"Job Title": "Clown",
"Organisation": "Big Comp INC",
"Organisation Type": "Company",
"Latitude": "54.669978",
"Longitude": "-1.452469"
},]