What I'm trying to do is loop through an array of items and add each of them to my db. After they're added to the db, the 'Add' class' function should return the status and message of each item. I'm trying to add each of these statuses and messages to an outcome
array that's outside my foreach
loop and then when the loop completes I'm trying to print the outcome
array but I keep getting this error:
Trying to get property of non-object in <b>pathToTheLoop\ScheduleController.php</b> on line <b>38</b><br />
. The line which is specified points to where I'm trying to add the stuff to my outcome
array.
Anyway, here's my code:
$returned = array();
foreach ($dates as $date) {
$startStr = $date . " " . $start;
$startDate = DateTime::createFromFormat('d/m/Y H:i:s', $startStr);
$endStr = $date . " " . $end;
$endDate = DateTime::createFromFormat('d/m/Y H:i:s', $endStr);
$sdl = new Schedule($startDate, $endDate, null);
$outcome = $sdl->createSlot();
$returned['status'] = $outcome->status;
$returned['message'] = $outcome->message;
}
print json_encode($returned);
Note that I've tried populating my array with the method above, as well as with the following method:
$returned[] = array(
$status => $outcome->status,
$message => $outcome->message
);
I'm not including the code for the Schedule
class because I'm fairly certain that it works because stuff I send to add to the db actually gets added and also because if I replace the part where I'm trying to populate the array with a print // the outcomes
it actually prints it.
Edit: When I do var_dump($outcome);
instead of trying to add the outcomes to $returned
I get this:
array(2) {
["status"]=>
string(7) "success"
["message"]=>
string(62) "New slot added from 2016-10-10 09:00:01 to 2016-10-10 10:00:00"
}
array(2) {
["status"]=>
string(7) "success"
["message"]=>
string(62) "New slot added from 2016-10-17 09:00:01 to 2016-10-17 10:00:00"
}
array(2) {
["status"]=>
string(7) "success"
["message"]=>
string(62) "New slot added from 2016-10-24 09:00:01 to 2016-10-24 10:00:00"
}
array(2) {
["status"]=>
string(7) "success"
["message"]=>
string(62) "New slot added from 2016-10-31 09:00:01 to 2016-10-31 10:00:00"
}