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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?