dongmi5015 2016-10-16 06:46
浏览 41
已采纳

php将项添加到foreach循环之外的数组中

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"
}
  • 写回答

2条回答 默认 最新

  • douyasihefu6214 2016-10-16 06:59
    关注

    It appears that the value of $outcome is an array rather than an object so you could try casting the result as an object and that should allow you to use the object notation to access the values.

      $outcome = (object)$sdl->createSlot();
      $returned['status'] = $outcome->status;
      $returned['message'] = $outcome->message;
    

    or, alternatively use the array notation

      $outcome = $sdl->createSlot();
      $returned['status'] = $outcome['status'];
      $returned['message'] = $outcome['message'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统