0
$events = array();
$employeeData = array();
foreach ($rows as $fetch)
{ 
 $employeeData['start_date'] = $fetch->start_date;
 $employeeData['events']['start_time'] = $fetch->start_time;
 $employeeData['events']['end_time'] = $fetch->end_time;
 $employeeData['events']['name'] = $fetch->FirstName. ' ' .$fetch->LastName;
 array_push($events, $employeeData);
}
$success_status = array(
 'events'=> $events
);
echo json_encode($success_status, true);

Result------------

{ "events": [{ "start_date": "2018-05-27", "events": { "start_time": "11:45:00", "end_time": "13:00:00", "name": "Demo" } }, { "start_date": "2018-06-29", "events": { "start_time": "15:30:00", "end_time": "15:45:00", "name": "Demo" } }, { "start_date": "2018-06-29", "events": { "start_time": "16:30:00", "end_time": "23:45:00", "name": "Demo" } }] }

I am getting the above result but I need to get the following result

{ "events": [{ "start_date": "2018-05-27", "events": [{ "start_time": "11:45:00", "end_time": "13:00:00", "name": "Demo" }] }, { "start_date": "2018-06-29", "events": [{ "start_time": "15:30:00", "end_time": "15:45:00", "name": "Demo" }, { "start_time": "16:30:00", "end_time": "23:45:00", "name": "Demo" }] }] }

Dave
5,21717 gold badges34 silver badges45 bronze badges
asked Oct 5, 2018 at 16:24

2 Answers 2

1

I think this should produce the output your after, it's a case of adding an extra layer of arrays to the data you want nested in [{...}]...

$employeeData['events'][] = ['start_time' => $fetch->start_time, 
 'end_time' =>$fetch->end_time,
 'name' => $fetch->FirstName. ' ' .$fetch->LastName ];

So this uses [] to just add the data to an array and I've wrapped the elements into one array rather than adding it as items.

answered Oct 5, 2018 at 16:48
Sign up to request clarification or add additional context in comments.

Comments

0

$employeeData['events']['start_time'] basically is creating a single item as an associative array. What you want is an array of event items. So you need to put the event data into an array, then add that array to your events array, like this:

$events = array();
$employeeData = array();
foreach ($rows as $fetch)
{ 
 $event = [];
 $employeeData['start_date'] = $fetch->start_date;
 $event['start_time'] = $fetch->start_time;
 $event['end_time'] = $fetch->end_time;
 $event['name'] = $fetch->FirstName. ' ' .$fetch->LastName;
 $employeeData['events'][] = $event;
 array_push($events, $employeeData);
}
$success_status = array(
 'events'=> $events
);
echo json_encode($success_status, true);
answered Oct 5, 2018 at 16:43

3 Comments

Just a guess, you didn't explain what you did in your code at all. I would classify this as a code-only answer which tend to get downvotes.
Ok. It would be nice if that was pointed out. It seems like there was another answer submitted and a downvote just to push my answer below it... I'll edit to include an explanation though.
Perhaps not. Just seemed like they both came right at the same time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.