0

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();
2
  • What do you mean by "array of multiple data"? Can you give an example of that data? Commented Jun 27, 2018 at 13:13
  • 1
    You can't put function in array, follow the answer of @DanyalSandeelo Commented Jun 27, 2018 at 13:22

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

doesnt work because whe don't have the ";" at the end of each line for $dataArray[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.