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 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误