把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();

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();