duanan6043 2018-06-27 04:55
浏览 14
已采纳

把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'))
       ->setRows(array(
                 foreach ($productsCursor as $product) {
                      array($product->getId(), $product->getCreated()->format('Y-m-d H:i:s')),
                 }
         ));
            $table->render();
  • 写回答

1条回答 默认 最新

  • dpzff20644 2018-06-27 05: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);
    $table->render();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?