title: ArrayToTextTable meta:

  • name: description content: Used to output table information.
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Component Library|Miscellaneous Tools

ArrayToTextTable

Use

Used to convert array data to table output.

Core Object Class

To implement this component function you need to load the core class: EasySwoole\Utility\ArrayToTextTable

Core Object Method

getTable

Get the form

  • mixed $data table data
  1. Public function getTable($data = null)

setIndentation

Set table indentation

  • mixed $indentation setting indentation
  1. public function setIndentation($indentation)

isDisplayHeader

Set the table header

  • bool $displayHeader Do you need a table header?
  1. Public function isDisplayHeader(bool $displayHeader)

setKeysAlignment

Set table header alignment

  • mixed $keysAlignment table header alignment
  1. Public function setKeysAlignment($keysAlignment)

setValuesAlignment

Set table data alignment

  • mixed $keysAlignment table header alignment
  1. Public function setValuesAlignment($valuesAlignment)

setFormatter

Processing tabular data format

  • mixed $formatter data mode
  1. public function setFormatter($formatter)

how to use

Creating objects of the core class

  1. $data = [
  2. [
  3. 'name' => 'James',
  4. 'age' => '20',
  5. 'sex'=>'man'
  6. ],
  7. [
  8. 'name' => 'Tony',
  9. 'age' => 50,
  10. 'email' => '291323003@qq.com',
  11. ],
  12. ];
  13. // Create a core class object, and bring in the data parameter $data
  14. $renderer = new \EasySwoole\Utility\ArrayToTextTable($data);
  15. // Set the table indentation
  16. $renderer->setIndentation("\t");
  17. // Set the table header
  18. $renderer->isDisplayHeader(true);
  19. // Set the table header alignment
  20. $renderer->setKeysAlignment(\EasySwoole\Utility\ArrayToTextTable::AlignLeft);
  21. // Set the table data alignment
  22. $renderer->setValuesAlignment(\EasySwoole\Utility\ArrayToTextTable::AlignLeft);
  23. // Processing table data format
  24. $renderer->setFormatter(function (&$value,$key){
  25. if($key == 'sex'){
  26. if(empty($value)){
  27. $value = 'Unknown gender';
  28. }
  29. }else if($key == 'email'){
  30. if(empty($value)){
  31. $value = 'Unknown mailbox';
  32. }
  33. }
  34. });
  35. $table = $renderer->getTable();
  36. echo $renderer;

::: tip

​ Ps: Please run in the command line mode. If the border of the table is not aligned, check whether the space ratio of the Chinese font and the English font is 2:1.

:::