2018-06-27 12:55 阅读 13

把foreach放到array / php中

With my symfony command, I want to display an array of multiple data. How to fill the fields of my table in an automatic way as I tried to do in my example?

$table = new Table($output);
 $table->setHeaders(array('ID', 'Date'))
                 foreach ($productsCursor as $product) {
                      array($product->getId(), $product->getCreated()->format('Y-m-d H:i:s')),
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dpzff20644 dpzff20644 2018-06-27 13:17

    You can iterate over the loop outside the declaration and then use it inside like this:

    $dataArray= array()
    foreach ($productsCursor as $product) {
       $dataArray[] =   array($product->getId(), $product->getCreated()->format('Y-m-d H:i:s'));

    and then you can set it as:

    $table = new Table($output);
    $table->setHeaders(array('ID', 'Date'))->setRows($dataArray);
    点赞 评论 复制链接分享