I want to be able to encode JSON into a specific format so that it can be used with another script that I have created. I am able to encode it with PHP into JSON but need to be able to push these encoded JSON sets to a 'named' array.
At the moment I am getting this
{
"ann_date":"14\/12\/2017",
"ann_title":"Return Dates",
"ann_content":"Have a good break. The course timetable resumes on Wednesday 3rd January 2018",
"tutor":"John Smith"
}
From this code
class AnnData {
public $ann_date = "";
public $ann_title = "";
public $ann_content = "";
public $tutor = "";
}
while($row = mysqli_fetch_array($result)) {
$ann_date = $row['ann_date'];
$ann_title = $row['ann_title'];
$ann_content = $row['ann_content'];
$tutor = $row['tutor'];
$annData = new AnnData();
$annData->ann_date = $ann_date;
$annData->ann_title = $ann_title;
$annData->ann_content = $ann_content;
$annData->tutor = $tutor;
$annQ = json_encode($annData);
But need it to look like this
{
"announcements":[{
"ann_date":"14\/12\/2017",
"ann_title":"Return Dates",
"ann_content":"Have a good break. The course timetable resumes on Wednesday 3rd January 2018",
"tutor":"John Smith"}]
}
$result['announcements'][] = $annData… You simply need to build the actual data structure you want…$result = array(); $result['announcements'] = $annData; $annQ = json_encode($result);, is it the result you want?