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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持